Golang Map Addressability
http://wangzhezhe.github.io/blog/2016/01/22/golangmapaddressability-dot-md/
在golang中关于map可达性的问题(addresable?)
在go playground中有这样的例子:
1 |
|
这样在执行的时候会报一个常见的错误:cannot assign to items["q"].A
改变一下value的声明方式,之后再进行类似的操作就可以了:
1 |
|
在golang设计的时候,map中的value值应该是地址不可达的,就是说直接取map中的元素的地址会报错,比如把上面例子中的main函数改成下边这样:
1 |
|
会报出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的。
Golang Map Addressability的更多相关文章
- 【GoLang】GoLang map 非线程安全 & 并发度写优化
Catena (时序存储引擎)中有一个函数的实现备受争议,它从 map 中根据指定的 name 获取一个 metricSource.每一次插入操作都会至少调用一次这个函数,现实场景中该函数调用更是频繁 ...
- golang map输出排序
由于GoLang Map 内部存储是无序的,当需要按顺序获得map存储的key -value值时,应该对遍历出来的结果进行重新排序: 在go 1.8版本后,提供的slice sort 功能使排序更简单 ...
- Golang Map实现(一)
本文学习 Golang 的 Map 数据结构,以及map buckets 的数据组织结构. hash 表是什么 从大学的课本里面,我们学到:hash 表其实就是将key 通过hash算法映射到数组的某 ...
- Golang Map实现(四) map 的赋值和扩容
title: Golang Map 实现 (四) date: 2020-04-28 18:20:30 tags: golang map 操作,是map 实现中较复杂的逻辑.因为当赋值时,为了减少has ...
- golang map getkeys
golang 获取map的keys package main import "fmt" import "reflect" func main() { abc : ...
- golang map
Our friend Monk has been made teacher for the day today by his school professors . He is going to te ...
- golang map 读写锁与深度拷贝的坑
0X01 golang中,map(字典)无法并发读写 简单来说,新建万条线程对同一个map又读又写,会报错. 为此,最好加锁,其实性能影响并不明显. type taskCache struct{ sy ...
- Golang map 如何进行删除操作?
Cyeam 关注 2017.11.02 10:02* 字数 372 阅读 2784评论 0喜欢 3 map 的删除操作 Golang 内置了哈希表,总体上是使用哈希链表实现的,如果出现哈希冲突,就把冲 ...
- Golang map并发 读写锁
golang并发 一:只有写操作 var ( count int l = sync.Mutex{} m = make(map[int]int) ) //全局变量并发写 导致计数错误 func vari ...
随机推荐
- Apache Log4j 2 is Coming
刚刚从同事那里得知,log4j 2 出beta版本了. 有啥提升呢? Improved PerformanceLog4j 2 contains next-generation Asynchronous ...
- [python学习篇][廖雪峰][1]高级特性--创建生成器 方法2 yield
def fib(max): n, a, b = 0, 0, 1 while n < max: print b a, b = b, a + b n = n + 1 将print b 改成yield ...
- linux 系统时间调整
linux的硬件时间是从COMS中读取的. 系统时间是由操作系统维护的. 先查看时区是否正确 (东八区 +8) #date -R 选择时区: #tzselect 修改了系统时间,还应该跟硬件时间进行同 ...
- 大数相减 C语言
#include <stdio.h> #include <string.h> using namespace std; ],b[]; void Sub() { ; if(a = ...
- 九度oj 题目1357:疯狂地Jobdu序列
题目描述: 阳仔作为OJ的数据管理员,每一周的题目录入都让其很抓狂,因为题目不是他出的,他控制不了出题的速度……在等题目的时候,阳仔又不敢出去打篮球,所以只能在纸上乱涂乱写,这天,阳仔在纸上写下了这样 ...
- POJ——1611The Suspects(启发式并查集+邻接表)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 31100 Accepted: 15110 Descri ...
- 升级springboot 2.x 踩过的坑——跨域导致session问题
目前IT界主流前后端分离,但是在分离过程中一定会存在跨域的问题. 什么是跨域? 是指浏览器从一个域名的网页去请求另一个域名的资源时,域名.端口.协议任一不同,都是跨域. 做过web后台的童鞋都知道,跨 ...
- linux上部署thinkphp5提示500
解决方法一:LNMP 1.4上也可以直接使用lnmp1.4/tools/ 目录下的 ./remove_open_basedir_restriction.sh,输入网站的全路径(如/www/wwwroo ...
- chroot下二进制程序迁移
#!/bin/bash # #define function#Copy binary programcp_bin(){ cmd_dir=${cmd_path%/*} [ ! -d /mnt/sysro ...
- hdu 1401(单广各种卡的搜索题||双广秒速)
Solitaire Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...