插入地图时,Go(深层)是否会复制密钥?

I have a map with complex keys - for example, 2D arrays:

m := make(map[[2][3]int]int)

When I insert a new key into the map, does Go make a deep copy of the key?

a := [2][3]int{{1, 2, 3}, {4, 5, 6}}
m[a] = 1

In other words, if I change the array a after using it as a map key, does the map still contain the old value of a?

Arrays are always passed by value, so, yes in this case Go will make a deep copy of the key.

From the language spec

The comparison operators == and != must be fully defined for operands of the key type; thus the key type must not be a function, map, or slice. If the key type is an interface type, these comparison operators must be defined for the dynamic key values; failure will cause a run-time panic.

The keys are copied into the map. Excluding map and slice as valid keys means that the keys can't change. Note that go doesn't follow pointers if you define a map type with a pointer as a key (eg map[*int]int) it compares the pointers directly.

Short answer, it is copied.

By specification, Arrays are value types.

Go's arrays are values. An array variable denotes the entire array; it is not a pointer to the first array element (as would be the case in C). This means that when you assign or pass around an array value you will make a copy of its contents. (To avoid the copy you could pass a pointer to the array, but then that's a pointer to an array, not an array.) https://blog.golang.org/go-slices-usage-and-internals

See for yourself:

https://play.golang.org/p/fEUYWwN-pm

package main

import (
    "fmt"
)

func main() {
    m := make(map[[2][3]int]int)
    a := [2][3]int{{1, 2, 3}, {4, 5, 6}}

    fmt.Printf("Pointer to a: %p
", &a)

    m[a] = 1
    for k, _ := range m {
        fmt.Printf("Pointer to k: %p
", &k)
    }
}

The pointers do not match.

EDIT: The real reason is when inserting into a map, the key value is copied. Or, you can continue to just remember the rule above: arrays are value types and their reuse denotes a copy. Either works here. :)