In Go, you can split a string using the strings.Split
function.
In this example,
strings
package.str
with a value of "Hello World"
.strings.Split
function with a space as the delimiter.package main
import (
"fmt"
"strings"
)
func main() {
str := "Hello World"
parts := strings.Split(str, " ")
fmt.Println(parts)
}
[Hello World]
In this example,
strings
package.str
with a value of "apple,banana,cherry"
.strings.Split
function with a comma as the delimiter.package main
import (
"fmt"
"strings"
)
func main() {
str := "apple,banana,cherry"
parts := strings.Split(str, ",")
fmt.Println(parts)
}
[apple banana cherry]
In this tutorial, we learned How to Split a String in Go language with well detailed examples.