我想根據字符串爲某些頻道編制索引。我正在使用地圖,但不允許我爲其分配頻道。我不斷收到「恐慌:分配到無地圖入口」,我錯過了什麼?Golang。頻道地圖
package main
import "fmt"
func main() {
var things map[string](chan int)
things["stuff"] = make(chan int)
things["stuff"] <- 2
mything := <-things["stuff"]
fmt.Printf("my thing: %d", mything)
}
https://play.golang.org/p/PYvzhs4q4S
你用'make'或者地圖文字初始化地圖。也許要經過[「Go of Go」](https://tour.golang.org/),它涵蓋了所有的基礎知識。 – JimB
只是一個小小的說明,以防萬一:地圖的零值爲零。正如答案所述,你需要用'make'來初始化它# – threeve