In Go, you can split a string by comma using the strings.Split
function.
In this example,
str
with a value containing commas.strings.Split
function to split the string into an array of substrings using comma as the delimiter.package main
import (
"fmt"
"strings"
)
func main() {
str := "apple,banana,orange"
substrings := strings.Split(str, ",")
for _, sub := range substrings {
fmt.Println(sub)
}
}
apple banana orange
In this tutorial, we learned How to Split a String by Comma in Go language with well detailed examples.