此代碼(位于Go Playground的http://play.golang.org/p/BjWPVdQQrS):package mainimport "fmt"type foodStruct struct { fruit map[int]string veggie map[int]string}func showFood(f map[int]map[int]string) { fmt.Println(f[1][1])}func main() { f := map[int]foodStruct{ 1: { fruit: map[int]string{1: "pear"}, veggie: map[int]string{1: "celery"}, }, } fmt.Println(f[1].fruit[1]) g := map[int]map[int]string{1: map[int]string{1: "orange"}} showFood(g) // showFood(f.fruit) // Compile error: "f.fruit undefined (type map[int]foodStruct has no field or method fruit)"}印刷:pearorange有什么辦法可以將變量f的形式傳遞給showFood(),以使其顯示“梨”?傳遞f.fruit會(huì)引發(fā)上面注釋行中所示的編譯錯(cuò)誤。這個(gè)錯(cuò)誤令我感到困惑,因?yàn)閒oodStruct確實(shí)有野果。
傳遞結(jié)構(gòu)中的Go地圖
繁星點(diǎn)點(diǎn)滴滴
2021-05-11 21:05:18