How to split a string in Golang

The Split() method​ in Golang (defined in the strings library) breaks a string down into a list of substrings using a specified separator. The method returns the substrings in the form of a slice.

Syntax

This is how the function is defined in Golang:

func Split(str, sep string) []string
  • str is the string to be split.
  • The string splits at the specified separator, sep. If an empty string is provided as the separator, then the string is split at every character.
svg viewer

Code

The following code snippet shows how the Split method is used:

package main
import "fmt"
import "strings" // Needed to use Split
func main() {
str := "hi, this is, educative"
split := strings.Split(str, ",")
fmt.Println(split)
fmt.Println("The length of the slice is:", len(split))
}
New on Educative
Learn any Language for FREE all September 🎉
For the entire month of September, get unlimited access to our entire catalog of beginner coding resources.
🎁 G i v e a w a y
30 Days of Code
Complete Educative’s daily coding challenge every day in September, and win exciting Prizes.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved