#JavaInspires
How to copy elements from one Map to another Map in Go Lang ?
Program to copy elements one map to another map in go language.
Here, we are using for loop to copy each element to another map.
// golang program to copy elements one map to another map package main import "fmt" func main() { // create a map using shorthand declaration map1 := make(map[string]int) // add elements to the map map1["john"] = 22 map1["erik"] = 25 map1["sean"] = 24 map1["clark"] = 26 // print map1 fmt.Println("map1 :", map1) // create empty map map2 := make(map[string]int) // iterate map1 and copy each element to map2 for i, e := range map1 { map2[i] = e } // print map2 fmt.Println("map2 :", map2) }
PS C:\DEVD\WORK\golang\example-programs> go run .\copymapexample.go map1 : map[clark:26 erik:25 john:22 sean:24] map2 : map[clark:26 erik:25 john:22 sean:24] PS C:\DEVD\WORK\golang\example-programs>