Yes, you can definitely parse that string into a map of the type you specify!
Take a look at the "strings" package, in particular strings.SplitN(...)
. Consider these examples and think about how you can combine them, along with iteration, to populate that map:
str := "key:value key2:key:value"
kvs := strings.SplitN(str, " ", -1)
// []string{
// "key:value",
// "key2:key:value",
// }
kv1 := strings.SplitN(kvs[1], ":", 2)
// []string{
// "key2",
// "key:value",
// }
That is, you can split the string by spaces (
) to get each key/value pair, then you can split by colons (:
) to split each pair into their own parts. Finally, you just need to add each pair to the map!
Notice that when you call strings.SplitN(...)
with -1 as the final argument it will split the input string into as many substrings as are present:
kv1 := strings.SplitN(kvs[1], ":", -1) // Note the -1 here
// []string{
// "key2",
// "key",
// "value",
// }