Does (deep) copy keys do when pasted into a card?

I have one mapwith complex keys - for example, 2D arrays:

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

When I insert a new key into the card, 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 changed the array aafter using it as a map key, does the map still have the old value a?

+4
source share
2 answers

Short answer, it is copied.

By specification, arrays value types.

Go - . ; ( C). , . ( , , , ). https://blog.golang.org/go-slices-usage-and-internals

:

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\n", &a)

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

.

EDIT: , . : , . .:)

+6

, , Go .

== != ; , , . , ; .

. map slice , . , go , (, map[*int]int), .

+3
source

All Articles