In this section, we're going to learn how to merge maps. Check out the two maps in the following screenshot:
As you can see, there are four items, and the maps are basically mapping a string to an integer.
Note
If you don't use a comma, as shown after 22
in the preceding screenshot, you'll get a compile-time exception. This is because of the automatic addition of a semicolon in Go, which is not appropriate in this code.
Okay, so let's go ahead and merge these two maps together. Unfortunately, there's no built-in way of doing this, so all we have to do is just to iterate these two maps and then merge them together. Check out the following code:
package main import "fmt" func main(){ map1 := map[string]int { "Michael":10, "Jessica":20, "Tarik":33, "Jon": 22, } fmt.Println(map1) map2 := map[string]int { "Lord":11, "Of":22, "The":36, "Rings": 23, } for key, value := range map2{ map1[key] = value } fmt.Println(map1) }
The output for the...