Deep copying maps in Golang

2020-03-31 08:34发布

问题:

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.

回答1:

I think of 2 solutions.

1) Simply mashal it back, then copy the bytes.

2) Recursive calls as below.

func deepCopyJSON(src map[string]interface{}, dest map[string]interface{}) {
    for key, value := range src {
        switch src[key].(type) {
        case map[string]interface{}:
            dest[key] = map[string]interface{}{}
            deepCopyJSON(src[key].(map[string]interface{}), dest[key].(map[string]interface{}))
        default:
            dest[key] = value
        }
    }
}

https://play.golang.org/p/l6W1PD4lppG



回答2:

func deepCopyJSON(src map[string]interface{}, dest map[string]interface{}) error {
    if src == nil {
        return errors.New("src is nil. You cannot read from a nil map")
    }
    if dest == nil {
        return errors.New("dest is nil. You cannot insert to a nil map")
    }
    jsonStr, err := json.Marshal(src)
    if err != nil {
        return err
    }
    err = json.Unmarshal(jsonStr, &dest)
    if err != nil {
        return err
    }
    return nil
}