How to copy a map to another map?
To copy a map content need to execute a for loop and fetch the index value 1 by 1 with element and assign it to another map. Below is a short example.
Example
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)
}
}
Output
x => 1
y => 2
Most Helpful This Week
Example to create custom error
Example: Split, Join, and Equal from BYTES Package
How to import and alias package names?
Example of Sscan vs Sscanf vs Sscanln from FMT Package
How to check if a string contains a white space in Golang?
Regular expression to extract filename from given path in Golang
How to check UPPERCASE characters in a string in Golang?
Find length of Channel, Pointer, Slice, String and Map
Regular expression to validate the date format in "dd/mm/yyyy"
Golang String Concatenation