当对map只声明时,由于map为引用类型,所以默认值为nil,但对nil map 而言,支持read ,但不支持write 当执行write操作时, 会抛出panic异常;

代码如下:

func TestDeclareInit(t *testing.T) {
// 只声明
var m1 map[int]int // 初始化值为nil,但支持访问
t.Log(m1)
t.Log(m1[1]) // 输出了0
m1[0] = 0
}

  根据官网文档解释(并未说明具体的原因):https://blog.golang.org/maps

Map types are reference types, like pointers or slices, and so the value of m above is nil; it doesn't point to an initialized map. A nil map behaves like an empty map when reading, but attempts to write to a nil map will cause a runtime panic; don't do that. To initialize a map, use the built in make function:

m = make(map[string]int)

The make function allocates and initializes a hash map data structure and returns a map value that points to it. The specifics of that data structure are an implementation detail of the runtime and are not specified by the language itself. In this article we will focus on the use of maps, not their implementation.

"个人理解":

  当只声明未初始化一个map时,此时并未申请一个内存空间,因此当进行查找操作时,由于map容器的内存空间并不存在,因此本身就不会找到当前key的地址,所以直接返回value的默认值而不会报错;

  而对于写入操作时,写入操作必须在一块已经开辟的内存空间中进行操作,而nil map 实际数据存储空间并未开辟,因此当执行写入操作时就会抛出panic

在/src/runtime/map.go 中 存在 mapassign 方法(数据写入)

// Like mapaccess, but allocates a slot for the key if it is not present in the map.
func mapassign(t *maptype, h *hmap, key unsafe.Pointer) unsafe.Pointer {
if h == nil { // 在这里首先判断了 如果 *hmap为nil 代表当前hmap内存空间尚未分配
panic(plainError("assignment to entry in nil map"))
}
... 后面代码忽略
}

在src/runtime/map.go mapaccess1和mapaccess2方法中(数据读取)

func mapaccess2(t *maptype, h *hmap, key unsafe.Pointer) (unsafe.Pointer, bool) {
......忽略其他代码
if h == nil || h.count == 0 { //如果hmap没有分配内存或没有数据时直接返回零值
if t.hashMightPanic() {
t.hasher(key, 0) // see issue 23734
}
return unsafe.Pointer(&zeroVal[0]), false
}
...忽略其他代码
}

golang map学习的更多相关文章

  1. Golang 语法学习笔记

    Golang 语法学习笔记 包.变量和函数. 包 每个 Go 程序都是由包组成的. 程序运行的入口是包 main. 包名与导入路径的最后一个目录一致."math/rand" 包由 ...

  2. Golang Map实现(一)

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

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

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

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

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

  5. golang map输出排序

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

  6. golang语法学习(一):变量,常量以及数据类型

    学习一门新的语言肯定是要从他的主要的语法開始,语法构成了整个程序设计的基础,从语法中我们也能够看到这门语言的一些特性.可是话说回来.语法这东西,不同的语言大同小异,所以这也对语法的记忆造成了一定的难度 ...

  7. 大话图解golang map

    前言 网上分析golang中map的源码的博客已经非常多了,随便一搜就有,而且也非常详细,所以如果我再来写就有点画蛇添足了(而且我也写不好,手动滑稽).但是我还是要写,略略略,这篇博客的意义在于能从几 ...

  8. golang map getkeys

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

  9. STL的pair学习, map学习

    http://blog.csdn.net/calvin_zcx/article/details/6072286 http://www.linuxidc.com/Linux/2014-10/107621 ...

随机推荐

  1. IDEA编写Scala代码时自动显示变量类型

    设置方法如下:settins -->Editor--> Code Style --> scala --Type Annotations  勾选框选部分 测试效果

  2. three.js 制作逻辑转体游戏(下)

    上一篇已经对绕非定轴转动有所了解,这篇郭先生继续说一说逻辑转体游戏的制作,这部分我们同样会遇到一些小问题,首先是根据数据渲染陷阱和目标区域,然后是对可以转动的判定,最后是获胜的判定. 1. 根据数据渲 ...

  3. 区块链入门到实战(35)之Solidity – 变量作用域

    局部变量的作用域仅限于定义它们的函数,但是状态变量可以有三种作用域类型. Public – 公共状态变量可以在内部访问,也可以通过消息访问.对于公共状态变量,将生成一个自动getter函数. Inte ...

  4. 5G边缘计算:开源架起5G MEC生态发展新通路

    摘要:‍‍本文尝试从‍‍边缘计算的角度来阐述了‍‍为什么‍‍要把边缘计算当做一种新的生产关系来构建,‍‍以及如何用开源来构建这种新的生产关系. 5G推动新一轮工业革命 过去‍‍人类经历了三次工业革命, ...

  5. 快速幂 (C++)

    typedef long long LL; using namespace std; //求a^b%m,递归写法 LL binaryPow(LL a,LL b,LL m){ if(b==){ //如果 ...

  6. C++STL complex吃书使用指南

    说在前面: complex即为复数 使用c++自带的complex类型,首先要有<complex>头文件,还要使用std命名空间 声明方式: complex <T> a:  声 ...

  7. Ignatius and the Princess IV (水题)

    "OK, you are not too bad, em... But you can never pass the next test." feng5166 says.  &qu ...

  8. Oracle数据库时提示“ORA-28000

    当使用SQL*Plus登录时,Oracle数据库时提示“ORA-28000:帐号被锁定”. 导致出现改错误的原因是:在oracle database 11g中,默认在default概要文件中设置了“F ...

  9. 16_Python的包package

    1.包的概述 1.包是将模块一文件夹的组织形式进行分组管理的方法,一系列模块进行分类管理有利于防止命名冲突 2.包是一个包含多个模块的特色目录,目录下有一个特色的文件__init__.py 3.包的命 ...

  10. SQL分词器1.10版

    处理SQL及分词效果: select * from ( select rownum as rn,tb1.stuid,tb1.summary from ( select stuid,sum(score) ...