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://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的更多相关文章

  1. 【GoLang】GoLang map 非线程安全 & 并发度写优化

    Catena (时序存储引擎)中有一个函数的实现备受争议,它从 map 中根据指定的 name 获取一个 metricSource.每一次插入操作都会至少调用一次这个函数,现实场景中该函数调用更是频繁 ...

  2. golang map输出排序

    由于GoLang Map 内部存储是无序的,当需要按顺序获得map存储的key -value值时,应该对遍历出来的结果进行重新排序: 在go 1.8版本后,提供的slice sort 功能使排序更简单 ...

  3. Golang Map实现(一)

    本文学习 Golang 的 Map 数据结构,以及map buckets 的数据组织结构. hash 表是什么 从大学的课本里面,我们学到:hash 表其实就是将key 通过hash算法映射到数组的某 ...

  4. Golang Map实现(四) map 的赋值和扩容

    title: Golang Map 实现 (四) date: 2020-04-28 18:20:30 tags: golang map 操作,是map 实现中较复杂的逻辑.因为当赋值时,为了减少has ...

  5. golang map getkeys

    golang 获取map的keys package main import "fmt" import "reflect" func main() { abc : ...

  6. golang map

    Our friend Monk has been made teacher for the day today by his school professors . He is going to te ...

  7. golang map 读写锁与深度拷贝的坑

    0X01 golang中,map(字典)无法并发读写 简单来说,新建万条线程对同一个map又读又写,会报错. 为此,最好加锁,其实性能影响并不明显. type taskCache struct{ sy ...

  8. Golang map 如何进行删除操作?

    Cyeam 关注 2017.11.02 10:02* 字数 372 阅读 2784评论 0喜欢 3 map 的删除操作 Golang 内置了哈希表,总体上是使用哈希链表实现的,如果出现哈希冲突,就把冲 ...

  9. Golang map并发 读写锁

    golang并发 一:只有写操作 var ( count int l = sync.Mutex{} m = make(map[int]int) ) //全局变量并发写 导致计数错误 func vari ...

随机推荐

  1. Django模板(filter过滤器{{ }}与tag标签{% %}应用)

     模板里面过滤器与标签的应用 templates模板里面的应用参考(主要应用在这里面) <!DOCTYPE html> <html lang="en"> & ...

  2. Frequent values(ST)

    描述 You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In addition to ...

  3. 【Luogu】P2473奖励关(期望DP)

    题目链接 逆推期望DP.设f[i][j]为1~i-1中吃到的宝物集合为j,在i~k轮能得到的最大期望分数. 如果不吃显然f[i][j]+=f[i+1][j]/n 如果吃就是f[i][j]+=max(f ...

  4. [AtCoderContest015D]A or...or B Problem

    [AtCoderContest015D]A or...or B Problem 试题描述 Nukes has an integer that can be represented as the bit ...

  5. mybatis学习(十一)——springmvc++spring+mybatis整合

    做任何一个项目都以一个需求,这里先定义一下需求:利用三大框架查询酒店列表. 一.搭建开发环境 1.创建一个web项目 我这里用的是 jdk1.8+tomact7.0 2.创建hotel表 CREATE ...

  6. Java24种设计模式的优点、缺点和适用环境总结

    一.7个常用的面向对象设计原则 1.单一职责原则: 它是实现高内聚.低耦合的指导方针:一个对象应该只包含单一的职责,并且该职责被完整的封装在一个类中: 2.开闭原则: 指软件实体应尽量在不改变原有的代 ...

  7. SHUoj 字符串进制转换

    字符串进制转换 发布时间: 2017年7月9日 18:17   最后更新: 2017年7月9日 21:17   时间限制: 1000ms   内存限制: 128M 描述 Claire Redfield ...

  8. FZOJ Problem 2110 Star

                                                                                                        ...

  9. ubuntu 安装自启动管理

    #sudo apt-get update #sudo apt-get install sysv-rc-conf 运行:#sudo sysv-rc-conf 也可以直接加入启动程序,例如把 /etc/i ...

  10. 论epoll的实现

    论epoll的实现 上一篇博客 论select的实现 里面已经说了为什么 select 比较慢.poll 的实现和 select 类似,只是少了最大 fd 限制,如果有兴趣可以自己去看代码.我这里来简 ...