map 是在 Go 中将值(value)与键(key)关联的内置类型。通过相应的键可以获取到值。
package main import "fmt" func main(){ var a map[int]string = make(map[int]string) // int代表key,string代表value a[1] = "100" a[2] = "666" fmt.Println(a) fmt.Println(a[1]) //通过key取值 }
package main import "fmt" func main(){ var a =map[int]string{1:"10",2:"100"} fmt.Println(a) fmt.Println(a[1]) //通过key取值 }
package main import "fmt" func main(){ var a =map[int]string{1:"10",2:"100"} delete(a,1) fmt.Println(a) }
package main import "fmt" func main(){ var a =map[int]string{1:"10",2:"100"} test(a) fmt.Println(a) } func test(a map[int]string) { a[1] = "888" fmt.Println(a) }
package main func main() { map1 := map[string]int{ "one": 1, "two": 2, } map2 := map1 if map1 == map2 { } }
package main import "fmt" func main(){ var a =map[int]string{1:"10",0:"100",10:"999"} //for循环 //for i:=0;i<len(a);i++{ // fmt.Println(a[i]) //} //range循环 for k,v:=range a{ fmt.Println(k) fmt.Println(v) } }