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 }
------------------------
package main import ( "fmt" ) func main() { map1 := map [string]int{ "x" : 1 , "y" : 2 , } map2 := map [string]int{} /* Copy Content from Map1 to Map2*/ for index,element := range map1{ map2[index] = element } for index,element := range map2{ fmt.Println(index, "=>" ,element) } } |
原文:https://www.cnblogs.com/oxspirt/p/11279800.html