I am trying to convert a string ["211007@it_4","211008@it_4"]
, which is saved in MySQL database to an array of string to use it as an index value.
I can not find a good way to do this in Go.
I am trying to convert a string ["211007@it_4","211008@it_4"]
, which is saved in MySQL database to an array of string to use it as an index value.
I can not find a good way to do this in Go.
Your input looks like a JSON array with string elements. If that is so, simply use the encoding/json
package to unmarshal it into a []string
variable.
Example:
s := `["211007@it_4","211008@it_4"]`
var parts []string
if err := json.Unmarshal([]byte(s), &parts); err != nil {
fmt.Println(err)
}
fmt.Println("elements:", parts)
Output (try it on the Go Playground):
elements: [211007@it_4 211008@it_4]