Created
May 17, 2019 14:41
-
-
Save miguelmota/e88cb2778e9f1cbf2314dee600bc03f9 to your computer and use it in GitHub Desktop.
Golang copy map object properties to another map object (js destructing equivalent)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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