This is a tiny bit cheesy but I was having trouble making fmt.Sscanf grok the pattern, so I am just splitting again. And you may have been missing strconv
- strconv.Atoi
is a quick converter.
package main
import (
"fmt"
"strconv"
"strings"
)
func main() {
lsd := make(map[string]int)
toParse := "abc:5, foo:5"
parts := strings.Split(toParse, ", ")
for _, p := range parts {
results := strings.SplitN(p, ":", 2)
val, err := strconv.Atoi(results[1])
if err != nil {
panic(err) //probably want to do somethig better
}
lsd[results[0]] = val
}
fmt.Printf("%#v", lsd)
}
map[string]int{"abc":5, "foo":5}