Skip to content

Instantly share code, notes, and snippets.

@miguelmota
Created May 17, 2019 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miguelmota/e88cb2778e9f1cbf2314dee600bc03f9 to your computer and use it in GitHub Desktop.
Save miguelmota/e88cb2778e9f1cbf2314dee600bc03f9 to your computer and use it in GitHub Desktop.
Golang copy map object properties to another map object (js destructing equivalent)
package main
import "fmt"
func main() {
person := map[string]interface{}{
"name": "test",
"age": 13,
"school": "stanford",
}
student := map[string]interface{}{
"school": "harvard",
}
for k, v := range person {
if _, ok := student[k]; !ok {
student[k] = v
}
}
fmt.Println(student) // map[age:13 name:test school:harvard]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment