To split a string into an array of words in Go, you can use the strings.Fields() function. This function splits a string into words (using white space characters as delimiters) and returns an array of strings.
Here's an example code that demonstrates how to use strings.Fields() function:
main.go165 chars13 lines
Output:
main.go47 chars2 lines
In this example, we first declare a string variable str
, which contains a sentence. We then pass this string to the strings.Fields()
function, which splits the string into individual words and returns an array of strings. Finally, we print the array of words using the fmt.Println()
function.
gistlibby LogSnag