If I have a nested map variable like this inside a struct:
type someStruct struct {
nestedMap map[int]map[string]string
}
var ss = someStruct {
nestedMap: make(map[int]map[string]string),
}
This does not work and does a runtime error.
How do I initialize it?
You have to make the child maps as well.
func (s *someStruct) Set(i int, k, v string) {
child, ok := s.nestedMap[i]
if !ok {
child = map[string]string{}
s.nestedMap[i] = child
}
child[k] = v
}
Initilize nested map like this:
temp := make(map[string]string,1)
temp ["name"]="Kube"
ss.nestedMap [2] = temp
fmt.Println(ss)