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
|
|
这样在执行的时候会报一个常见的错误:cannot assign to items["q"].A
改变一下value的声明方式,之后再进行类似的操作就可以了:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
|
在golang设计的时候,map中的value值应该是地址不可达的,就是说直接取map中的元素的地址会报错,比如把上面例子中的main函数改成下边这样:
1
2
3
4
|
|
会报出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
http://stackoverflow.com/questions/13101298/calling-a-pointer-method-on-a-struct-in-a-map https://golang.org/ref/spec#Address_operators
http://stackoverflow.com/questions/16059038/can-not-assign-to-a-struct-member-from-map
首先要明确一下,在声明的时候&Type{}与Type{}的区别在哪里
currently map member并非是addressable的。
相关文章
- com.alibaba.fastjson把JSONObject转换为Map<String, String>对象
- [Go] Map
- [React] Cleanly Map Over A Stateless Functional Component with a Higher Order Component
- PHP中extract() array_map call_user_func compact
- 从两个例子看 Python【map、reduce、filter】内置函数的使用
- [Javascript] Broadcaster + Operator + Listener pattern -- 9. Create modify, filter, map operators
- 微信小程序----map路线规划
- thymeleaf报错:Could not load content for xxx /css/bootstrap.min.css.map:HTTP error: status
- scala集合fold实例:将两个Map合并
- ATITIT提升效率 保持简单性 优化 简化 目录 1.1. 概念简单1 1.2. 语言简单性 弱类型 动态变量 动态实体1 1.3. 数据结构简单 arr 代替了array map s
- 利用BeanMap进行对象与Map的相互转换
- [手游项目3]-9-Go语言sync.Map(在并发环境中使用的map)
- map类型转string必须用.toString
- Java笔记:Map从入门到性能分析
- 006-ant design -结合echart-地址map市
- golang map 中的 key 为什么是无序的
- golang_并发安全: slice和map并发不安全及解决方法
- 【GoLang】golang context channel 详解
- golang下Map的使用和性能分析(勿滥用锁)
- golang中,map作为函数参数是如何传递的
- golang 多级json转map