panic: assignment to entry in nil map
轉載自:https://github.com/kevinyan815/gocookbook/issues/7
golang中map是引用型別,應用型別的變數未初始化時預設的zero value是nil。直接向nil map寫入鍵值資料會導致執行時錯誤
panic: assignment to entry in nil map
看一個例子:
package main
const alphabetStr string = "abcdefghijklmnopqrstuvwxyz"
func main() {
var alphabetMap map[string]bool
for _, r := range alphabetStr {
c := string(r)
alphabetMap[c] = true
}
}
執行這段程式會出現執行時從錯誤:
panic: assignment to entry in nil map
因為在宣告alphabetMap後並未初始化它,所以它的值是nil, 不指向任何記憶體地址。需要通過make方法分配確定的記憶體地址。程式修改後即可正常執行:
package main
import "fmt"
const alphabetStr string = "abcdefghijklmnopqrstuvwxyz"
func main() {
alphabetMap := make(map[string]bool)
for _, r := range alphabetStr {
c := string(r)
alphabetMap[c] = true
}
fmt.Println(alphabetMap["x"])
alphabetMap["x"] = false
fmt.Println(alphabetMap["x"])
}
關於這個問題官方文件中解釋如下:
This variable m is a map of string keys to int values:
var m map[string]int
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)
同為引用型別的slice,在使用append 向nil slice追加新元素就可以,原因是append方法在底層為slice重新分配了相關陣列讓nil slice指向了具體的記憶體地址
nil map doesn’t point to an initialized map. Assigning value won’t reallocate point address.
The append function appends the elements x to the end of the slice s, If the backing array of s is too small to fit all the given values a bigger array will be allocated. The returned slice will point to the newly allocated array.
相關文章
- 2022-07-24:以下go語言程式碼輸出什麼?A:[]int{};B:[]int(nil);C:panicGo
- Algorithm assignment 1Go
- Go 介面:nil介面為什麼不等於nil?Go
- go的 err!=nil 和 panic+recover 這兩種錯誤處理機制的關係和區別是什麼?Go
- Go 語言中,有時 nil 並不是一個 nilGo
- 自定義錯誤型別時應該注意的 nil !=nil型別
- Assignment Problem的若干思考
- Go“一個包含nil指標的介面不是nil介面”踩坑Go指標
- 【譯】defer-panic-and-recover
- HTML Entry 原始碼分析HTML原始碼
- 關於零值和nil
- Go 語言 nil 和介面Go
- GO 空指標和nilGo指標
- attempt to index local ‘result‘ (a nil value)Index
- Tkinter (05) 輸入部件 Entry
- go中panic原始碼解讀Go原始碼
- rust-quiz:006-value-of-assignment.rsRustUI
- Kafka分割槽分配策略(Partition Assignment Strategy)Kafka
- ERROR 1062 (23000): Duplicate entry for key 'PRIMARY'Error
- Gopher們寫if err != nil是否膩了?Go
- golang nil 切片和空切片區別Golang
- 翻譯|在Rust中怎樣panicRust
- Go基礎系列:defer、panic和recoverGo
- Tkinter (26) 輸入部件 ttk.Entry
- enq: TX - allocate ITL entry等待事件分析ENQ事件
- (*Type)(nil)有什麼特殊含義嗎?
- 【Go進階—基礎特性】panic 和 recoverGo
- Go 中的Defer,Panic 和 Recover 控制流Go
- 問題解決local variable 'xxx' referenced before assignment
- 什麼是 Angular library 的 secondary entry points?Angular
- tkinter中entry輸入控制元件(四)控制元件
- 關於enq: TX - allocate ITL entry等待事件ENQ事件
- 《伊始之地 Terra Nil》現已正式登陸Steam
- Swift 小心字典Value等於nil(容易出錯)Swift
- Golang 高效實踐之defer、panic、recover實踐Golang
- Evolutionary Computing: Notebook assignment - Traveling Salesman Problem 練習隨筆
- 【CS231n】Spring 2020 Assignments - Assignment1 - SoftmaxSpring
- 【ES6基礎】解構賦值(destructuring assignment)賦值Struct