使用方式:
test := make(map[int]string) test := map[int]string{ 1: "a", 2: "b", 3: "c", }
map 跟 struct 最大的差別在於:
map 的所有 key 要同型別,所有的value 也要同型別
test[123] = "as"
delete (test, 123)
test[123]
package main import ( "fmt" ) func main() { test := map[string]int{ "jason": 12, "eason": 15, } for idx := range test { fmt.Println(test[idx]) } }
package main import ( "fmt" ) func main() { var test = map[string]map[string]int{ "a": map[string]int{}, // 記得要逗點 } test["a"]["w"] = 123 fmt.Println(test) }
https://stackoverflow.com/a/44305711/4622645
Last updated 4 years ago
Was this helpful?