To iterate over words in a string in Go, you can use the strings.Fields
function to split the string into words based on whitespace.
In this example,
strings
package.str
with a value of 'Hello World Go'
.strings.Fields
function on the string to split it into words based on whitespace.package main
import (
"fmt"
"strings"
)
func main() {
str := "Hello World Go"
words := strings.Fields(str)
for _, word := range words {
fmt.Println(word)
}
}
Hello World Go
In this tutorial, we learned How to Iterate over Words in String in Go language with well detailed examples.