parse an array of strings in go

To parse an array of strings in Go, you can use the strings package and the Split function. The Split function takes two arguments: the original string and the separator. It splits the string into an array of strings based on the separator and returns the array.

Here's an example of how to parse an array of strings in Go using the Split function:

main.go
package main

import (
    "fmt"
    "strings"
)

func main() {
    str := "one,two,three,four,five"
    arr := strings.Split(str, ",")
    fmt.Println(arr)
}
159 chars
13 lines

This will output:

main.go
[one two three four five]
26 chars
2 lines

In this example, the str variable contains the original string. The Split function splits the string into an array of strings using the comma separator. The resulting array is stored in the arr variable. Finally, we print the array using the fmt.Println function.

related categories

gistlibby LogSnag