1. map中的lement一旦赋值后,是由map自身维护的一个单元,与原来的变量没有任何关系;
-
type A struct{
-
a int;
-
}
-
-
var test map[string]A;
-
test=make(map[string]A);
-
fmt.Println(unsafe.Sizeof(test));
-
a:=A{10}
-
test["hello"]=a;
-
a.a=11;
-
fmt.Println(test["hello"]) //此处a的值不会变化,仍为10
-
fmt.Println(unsafe.Sizeof(test));//添加元素前后,map的size不变;所以map的element实际存在另为的地方;
通过gdb调试发现,实际test就是一个指针,具体如下:
-
(gdb) whatis test
-
type = map[string]main.A
-
-
(gdb) p test
-
$1 = (map[string]main.A) 0xc200056100
-
-
-
(gdb) whatis *test
-
type = struct hash
-
-
(gdb) print *test
-
$4 = {count = 0, flags = 16, b = 0 '\000', keysize = 16 '\020',
-
valuesize = 8 '\b', bucketsize = 208, hash0 = 2050612719, buckets = 0x0,
-
oldbuckets = 0x0, nevacuate = 0}
阅读(1909) | 评论(0) | 转发(0) |