8 ways to split a string in Go with examples
Table of Contents
String splitting is a common task when parsing user input, environment variables, or CSV-like data. Go offers several standard-library tools for different splitting needs. Below are 8 practical options you can use today.
Split a string by separator #
To split a string in Go, use the strings.Split() function from the strings package. It splits a string into a list of substrings using the specified delimiter. The output substrings do not contain the separator.
The function signature is
func Split(s, sep string) []string
where
sis the string to splitsepis a separator (delimiter) by which the string is split
Example #
package main
import (
"fmt"
"strings"
)
func main() {
str := "strawberry, blueberry, raspberry"
fmt.Printf("strings.Split(): %#v\n", strings.Split(str, ", "))
}
Output:
strings.Split(): []string{"strawberry", "blueberry", "raspberry"}
Split a string without removing the separator #
To split a string in Go into substrings containing the separator by which they were split, use the strings.SplitAfter() function. It splits a string after each occurrence of the delimiter.
The functions signature:
func SplitAfter(s, sep string) []string
sis the string to splitsepis a separator (delimiter) by which the string is split
Example #
package main
import (
"fmt"
"strings"
)
func main() {
str := "strawberry, blueberry, raspberry"
fmt.Printf("strings.SplitAfter(): %#v\n", strings.SplitAfter(str, ", "))
}
Output:
strings.SplitAfter(): []string{"strawberry, ", "blueberry, ", "raspberry"}
Cut a string into 2 parts #
To cut a string on the first occurrence of the delimiter in Go, use the strings.Cut() function. It slices a string and returns the text before and after the separator.
The function signature:
func Cut(s, sep string) (before, after string, found bool)
The strings.Cut() takes a string s and a separator sep as arguments and splits the string s on the first occurrence of the sep. It returns the text before and after the sep, and the boolean value found indicating whether sep appears in the s.
Example #
package main
import (
"fmt"
"strings"
)
func main() {
str := "strawberry, blueberry, raspberry"
before, after, found := strings.Cut(str, ", ")
fmt.Printf("strings.Cut():\nbefore: %s\nafter: %s\nseparator found: %t\n", before, after, found)
}
Output:
strings.Cut():
before: strawberry
after: blueberry, raspberry
separator found: true
Split a string to at most n substrings #
To split a string in Go and receive at most n substrings, use the strings.SplitN() function. The last substring in this case will be the unsplit remainder.
The function signature:
func SplitN(s, sep string, n int) []string
The strings.SplitN() function works the same way as strings.Split() except that it finishes after n substrings.
Example #
package main
import (
"fmt"
"strings"
)
func main() {
str := "strawberry, blueberry, raspberry"
fmt.Printf("strings.SplitN(): %#v\n", strings.SplitN(str, ", ", 2))
}
Output:
strings.SplitN(): []string{"strawberry", "blueberry, raspberry"}
Split a string without removing the separator to at most n substrings #
To split a string in Go into output substrings containing a separator and getting at most n substrings, use the strings.SplitAfterN() function. It splits a string after each occurrence of the delimiter, and the last substring will be the unsplit remainder.
The function signature:
func SplitAfterN(s, sep string, n int) []string
The strings.SplitAfterN() function works the same way as strings.SplitAfter() except that it finishes after n substrings.
Example #
package main
import (
"fmt"
"strings"
)
func main() {
str := "strawberry, blueberry, raspberry"
fmt.Printf("strings.SplitAfterN(): %#v\n", strings.SplitAfterN(str, ", ", 2))
}
Output:
strings.SplitAfterN(): []string{"strawberry, ", "blueberry, raspberry"}
Split a string by white space characters #
To split a string by white space characters in Go, use the strings.Fields() function. It takes a string as an argument and splits it according to the white space characters defined by the unicode.IsSpace() function.
The function signature:
func Fields(s string) []string
Example #
package main
import (
"fmt"
"strings"
)
func main() {
str := "strawberry, blueberry, raspberry"
fmt.Printf("strings.Fields(): %#v\n", strings.Fields(str))
}
Output:
strings.Fields(): []string{"strawberry,", "blueberry,", "raspberry"}
Split a string by a splitting function #
To split a string according to a custom split function in Go, use the strings.FieldsFunc(). As arguments, it gets the string to split and the func(rune) bool function, which should return true if splitting should be done for a given rune.
The function signature:
func FieldsFunc(s string, f func(rune) bool) []string
Example #
In the example, the string is split on runes that are not Unicode letters.
package main
import (
"fmt"
"strings"
"unicode"
)
func main() {
str := "strawberry, blueberry, raspberry"
fmt.Printf("strings.FieldsFunc(): %#v\n", strings.FieldsFunc(str, func(r rune) bool {
return !unicode.IsLetter(r)
}))
}
strings.FieldsFunc(): []string{"strawberry", "blueberry", "raspberry"}
Split a string using the regexp #
In Go, you can also split a string using a Regular Expression. To do this, you must first create a new regular expression object Regexp, for example, by calling the regexp.MustCompile() function. The Regexp object has the Split() method that splits a given string s by the regular expression into at most n substrings (the last substring will be the unsplit remainder).
The method signature:
func (re *Regexp) Split(s string, n int) []string
Example #
package main
import (
"fmt"
"regexp"
)
func main() {
str := "strawberry,blueberry, raspberry"
regex := regexp.MustCompile(",\\s*")
fmt.Printf("Regexp.Split(): %#v\n", regex.Split(str, -1))
}
Output:
Regexp.Split(): []string{"strawberry", "blueberry", "raspberry"}
Common mistakes #
❌ Using strings.Split when you only need two parts:
parts := strings.Split("a=b", "=") // extra allocations
✅ Use strings.Cut for two pieces:
before, after, found := strings.Cut("a=b", "=")
❌ Splitting on whitespace with Split:
parts := strings.Split("a b", " ")
✅ Use strings.Fields to handle multiple spaces:
parts := strings.Fields("a b")
Best practices #
- Use
strings.Cutorstrings.SplitNwhen you want only the first separator. - Prefer
strings.Fieldsfor whitespace splitting; it handles multiple spaces and tabs. - Use
regexp.Splitonly when you truly need regex; it is slower than simple splits.
Related topics #
- Remove duplicate spaces in Go - normalize whitespace
- String to uppercase in Go - basic string transforms
- Concatenate strings in Go - build strings safely
- Remove non-alphanumeric characters in Go - cleaning input
- Compare strings in Go - equality and ordering
Tested with Go 1.25+ | Last verified: December 2025 🎉