How can I initialize a nested map inside a struct in Go? -


if have nested map variable inside struct:

type somestruct struct {     nestedmap map[int]map[string]string }  var ss = somestruct {     nestedmap: make(map[int]map[string]string), } 

this not work , runtime error.

how initialize it?

you have make child maps 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 } 

playground


Comments