To split a string into an array of substrings in Go, we can use the strings
package and the Split
function. The Split
function takes two arguments: the string to be split and a separator which defines where to split the string. It returns a slice of strings.
Here's an example code snippet to split a string into an array of substrings based on whitespace:
main.go259 chars17 lines
This will output:
main.go40 chars2 lines
We can also split a string based on a different separator, such as a comma, by changing the separator argument in the Split
function. For example:
main.go236 chars17 lines
This will output:
main.go22 chars2 lines
gistlibby LogSnag