I'm trying to compare the identity of 2 maps
package main
import "fmt"
func main() {
a := map[int]map[int]int{1:{2:2}}
b := a[1]
c := a[1]
// I can't do this:
// if b == c
// because I get:
// invalid operation: b == c (map can only be compared to nil)
// but as far as I can tell, mutation works, meaning that the 2 maps might actually be identical
b[3] = 3
fmt.Println(c[3]) // so mutation works...
fmt.Printf("b as pointer::%p\n", b)
fmt.Printf("c as pointer::%p\n", c)
// ok, so maybe these are the pointers to the variables b and c... but still, those variables contain the references to the same map, so it's understandable that these are different, but then I can't compare b==c like this
fmt.Printf("&b::%p\n", &b)
fmt.Printf("&c::%p\n", &c)
fmt.Println(&b == &c)
}
this produces the following output:
3
b as pointer::0x442280
c as pointer::0x442280
&b::0x40e130
&c::0x40e138
false
What's especially confusing to me is that while b as pointer::0x442280
and c as pointer::0x442280
seem to have the same values, which indeed would confirm to me my suspicion that those variables somehow point to the same dict.
So does anyone know how I can make go tell be that b
and c
are "the same object"?