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
Create and Print Multi Dimensional Slice in Golang
How to use a mutex to define critical sections of code and fix race conditions?
Replace first occurrence of string using Regexp
How to check UPPERCASE characters in a string in Golang?
How to compare equality of struct, slice and map?
Regular expression to extract text between square brackets
How to find length of Map in Go?
Functions mess (recursive anonymous function) in Golang
Runtime package variables
Split a character string based on change of character