How to Split a String in Golang?
Last Updated :
12 Jul, 2025
In Go language, strings differ from other languages like Java, C++, and Python. A string in Go is a sequence of variable-width characters, with each character represented by one or more bytes using UTF-8 encoding. In Go, you can split a string into a slice using several functions provided in the strings package. In this article,we will learn How to Split a String in Golang?
Example
package main
import (
"fmt"
"strings"
)
func main() {
str := "Welcome,to,GeeksforGeeks"
fmt.Println("Original String:", str)
}
Here, the string "Welcome,to,GeeksforGeeks" will be split using different methods. First, ensure you have imported the strings package to access the split functions.
Syntax
func Split(s, sep string) []string #Using Split Function
func SplitAfter(s, sep string) []string # Using SplitAfter Function
func SplitN(s, sep string, n int) []string # Using SplitN Function
func SplitAfterN(s, sep string, n int) []string # Using SplitAfterN Function
Using Split Function
The Split function divides a string into all substrings separated by the specified separator and returns a slice with these substrings.
Syntax
func Split(s, sep string) []string
s: The string to be split.sep: The separator. If sep is empty, it splits after each UTF-8 character. If both str and sep are empty, it returns an empty slice.
Example
Go
package main
import (
"fmt"
"strings"
)
func main() {
s := "Welcome,to,GeeksforGeeks"
fmt.Println("", s)
result := strings.Split(s, ",")
fmt.Println("Result:", result)
}
Output Welcome,to,GeeksforGeeks
Result: [Welcome to GeeksforGeeks]
SplitAfter Function
The SplitAfter function splits a string after each instance of the specified separator and returns a slice.
Syntax:
func SplitAfter(s, sep string) []string
Note:
- If
sep is empty, it splits after each UTF-8 sequence. - If
s doesn’t contain sep, it returns a slice of length 1 with s.
Example
Go
package main
import (
"fmt"
"strings"
)
func main() {
s := "Welcome,to,GeeksforGeeks"
fmt.Println("", s)
result := strings.SplitAfter(s, ",")
fmt.Println("Result:", result)
}
Output Welcome,to,GeeksforGeeks
Result: [Welcome, to, GeeksforGeeks]
SplitN Function
The SplitN function splits a string into a maximum number of substrings.
Syntax:
func SplitN(s, sep string, n int) []string
n > 0: Splits up to n substrings.n == 0: Returns an empty slice.n < 0: Returns all substrings.
Example:
Go
package main
import (
"fmt"
"strings"
)
func main() {
s := "Welcome,to,GeeksforGeeks"
fmt.Println("", s)
result := strings.SplitN(s, ",", 2)
fmt.Println("Result:", result)
}
Output Welcome,to,GeeksforGeeks
Result: [Welcome to,GeeksforGeeks]
SplitAfterN Function
The SplitAfterN function splits a string after each instance of the separator, but only up to n substrings.
Syntax
func SplitAfterN(str, sep string, n int) []string
- n > 0: Splits up to n substrings.
- n == 0: Returns an empty slice.
- n < 0: Returns all substrings.
Example:
Go
package main
import (
"fmt"
"strings"
)
func main() {
s := "Welcome,to,GeeksforGeeks"
fmt.Println("", s)
result := strings.SplitAfterN(s, ",", 2)
fmt.Println("Result using SplitAfterN:", result)
}
Output Welcome,to,GeeksforGeeks
Result using SplitAfterN: [Welcome, to,GeeksforGeeks]
Each of these functions provides a flexible way to split strings in Go, depending on the use case and the desired output structure. By understanding these functions, you can more effectively handle string manipulation in your Go programs.
Explore
Go Tutorial
3 min read
Overview
Fundamentals
Control Statements
Functions & Methods
Structure
Arrays
Slices
Strings
Pointers