To split a string into a slice of substrings based on a delimiter in Golang, you can use the strings.Split()
function from the strings
package. Here's an example:
package main
import (
"fmt"
"strings"
)
func main() {
str := "Hello, World! Welcome to Golang"
// Split the string into substrings based on the delimiter ","
substrings := strings.Split(str, ",")
// Print each substring
for _, s := range substrings {
fmt.Println(s)
}
}
Output:
Hello
World! Welcome to Golang
In this example, the strings.Split()
function splits the string str
into substrings based on the delimiter ","
. The resulting substrings are stored in the substrings
variable, which is a slice of strings.