From what I understand, maps are reference types in Go. So assignment will do shallow copy. I plan to do a recursive deep copy of Maps in golang. Recursive because I am dealing with a map that holds the unmarshalled contents of a JSON.
func deepCopyJSON(src map[string]interface{}, dest *map[string]interface{}) error {
if src == nil || dest == nil {
return errors.New("src/dest is nil. You cannot insert to a nil map")
}
for key, value := range src {
if reflect.TypeOf(value).String() != jsonType {
(*dest)[key] = value
} else {
(*dest)[key] = make(map[string]int)
//Suspect code below causes the error.
deepCopyJSON(value.(map[string]interface{}), &(((*dest)[key]).(map[string]interface{})))
}
}
return nil
}
The Error: cannot take the address of (*dest)[key].(map[string]interface {}) How do I get around this? Are there other ways to deep maps?
I primer on the internals of map in golang, will also be useful.