0
0
GoHow-ToBeginner · 3 min read

How to Copy Map in Go: Simple and Clear Guide

In Go, you copy a map by creating a new map and then copying each key-value pair from the original map using a loop. There is no built-in function to copy maps directly, so manual copying with a for loop is the standard way.
📐

Syntax

To copy a map in Go, you first create a new map with the same type as the original. Then, you use a for loop to copy each key and value from the original map to the new one.

  • make(map[KeyType]ValueType): creates a new empty map.
  • for key, value := range originalMap: loops over each item in the original map.
  • newMap[key] = value: assigns each key-value pair to the new map.
go
newMap := make(map[KeyType]ValueType)
for key, value := range originalMap {
    newMap[key] = value
}
💻

Example

This example shows how to copy a map of strings to integers. It creates an original map, copies it to a new map, and prints both to show they have the same content but are different maps.

go
package main

import "fmt"

func main() {
    original := map[string]int{"apple": 5, "banana": 3, "cherry": 7}

    // Create a new map
    copyMap := make(map[string]int)

    // Copy each key-value pair
    for key, value := range original {
        copyMap[key] = value
    }

    fmt.Println("Original map:", original)
    fmt.Println("Copied map:", copyMap)

    // Change copied map to show they are independent
    copyMap["apple"] = 10
    fmt.Println("After change, original map:", original)
    fmt.Println("After change, copied map:", copyMap)
}
Output
Original map: map[apple:5 banana:3 cherry:7] Copied map: map[apple:5 banana:3 cherry:7] After change, original map: map[apple:5 banana:3 cherry:7] After change, copied map: map[apple:10 banana:3 cherry:7]
⚠️

Common Pitfalls

One common mistake is to assign one map to another directly, like copyMap = originalMap. This does not copy the map but makes both variables point to the same map. Changes to one will affect the other.

Another pitfall is forgetting to create the new map with make before copying, which causes a runtime panic.

go
package main

import "fmt"

func main() {
    original := map[string]int{"x": 1}

    // Wrong: just assigns the reference
    copyMap := original
    copyMap["x"] = 2
    fmt.Println("Original after wrong copy:", original) // original changed too

    // Right: create new map and copy
    copyMap2 := make(map[string]int)
    for k, v := range original {
        copyMap2[k] = v
    }
    copyMap2["x"] = 3
    fmt.Println("Original after right copy:", original)  // original unchanged
    fmt.Println("CopyMap2:", copyMap2)
}
Output
Original after wrong copy: map[x:2] Original after right copy: map[x:2] CopyMap2: map[x:3]
📊

Quick Reference

  • Use make(map[KeyType]ValueType) to create a new map before copying.
  • Copy maps with a for loop over the original map.
  • Direct assignment copies the reference, not the data.
  • Maps are reference types in Go, so manual copying is needed for independent maps.

Key Takeaways

Maps in Go are reference types and must be copied manually with a loop.
Always create a new map with make() before copying key-value pairs.
Direct assignment copies the reference, not the map data.
Copying maps manually ensures changes to one map do not affect the other.