golang map学习

时间:2025-04-28 13:37:19

当对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
}
...忽略其他代码
}