Golang Map Addressability

时间:2023-03-10 03:05:56
Golang Map Addressability

http://wangzhezhe.github.io/blog/2016/01/22/golangmapaddressability-dot-md/

在golang中关于map可达性的问题(addresable?)

在go playground中有这样的例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package main

import (
"fmt"
) type Type struct {
A string
} func main() {
items := make(map[string]Type)
items["q"] = Type{}
items["q"].A = "abc"
fmt.Println(items)
}

这样在执行的时候会报一个常见的错误:cannot assign to items["q"].A

改变一下value的声明方式,之后再进行类似的操作就可以了:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package main

import (
"fmt"
) type Type struct {
A string
} func main() {
items := make(map[string]*Type)
items["q"] = &Type{}
items["q"].A = "abc"
fmt.Printf("%+v", items)
}

在golang设计的时候,map中的value值应该是地址不可达的,就是说直接取map中的元素的地址会报错,比如把上面例子中的main函数改成下边这样:

1
2
3
4
itemsb := make(map[string]Type)
itemsb["p"] = Type{3}
pointer := &itemsb["p"]
fmt.Println(pointer)

会报出cannot take the address of itemsb["p"]的错误。原因大致是因为,在golang中,一个容量不断增长的map可能会导致原来map中的一些元素发生rehashing,使得他们被重新分配到新的storage location上,这样可能会导致原先得到的address变得不可用。就是所谓的map member 的 not addresable。

正如这个答案中写的,map的indexing操作本来就是地址不可达的,这和golang中map的具体实现机制有关,golang中的map并没有保证它们的value值的地址是不可变的,因为value值的地址很有可能被重新分配,就像前面所说的那样。一个修改的办法就是把value值设置成为指针的形式,这样就相当于添加了一个额外的entry,即使真正需要的那个值的位置发生了变化,也可以redirection过去。以后使用map声明一个结构体的value值的时候,这一点要特别注意下。

对于slice的index操作就是地址可达的,对于map则是不可达的,总是使用map的时候要特别注意下。

??map is a reference to the map contents, but it does not hold references (unless it explicitly stores pointers).

相关参考:

https://golang.org/ref/spec#Address_operators

https://code.google.com/p/go/issues/detail?id=3117

https://groups.google.com/forum/#!topic/golang-nuts/4_pabWnsMp0

https://golang.org/ref/spec

http://*.com/questions/13101298/calling-a-pointer-method-on-a-struct-in-a-map https://golang.org/ref/spec#Address_operators

http://*.com/questions/16059038/can-not-assign-to-a-struct-member-from-map

首先要明确一下,在声明的时候&Type{}与Type{}的区别在哪里

currently map member并非是addressable的。