How to convert a string to a slice in Go

The Go standard library provides the strings.Split() method for the purpose of converting a single string to a slice of strings which is a common operation performed by developers. Here’s how to use it:

package main

import (

func main() {
	str := "I am a special one"
	fmt.Println(strings.Split(str, " ")) // ["I", "am", "a", "special", "one"]

The first argument to the Split() method is the string, and the second is the separator. The string is split into all substrings separated by the second argument to the function. If the separator does not exist in the string, a slice of only one element is returned (the string itself).

fmt.Println(strings.Split(str, ",")) // ["I am a special one"]

To convert the resulting slice of strings to a single string again, check out the strings.Join() method.

Thanks for reading, and happy coding!