Maps
map is built in the value type associated Go (value) of the key (key) is. Values can be acquired by a corresponding key. Maps similar to the dictionary in python
Maps definitions
maps the key value must be a hash (are immutable types: string, numeric ...) (the Map are unordered)
maps creation statement: var A Map [key type] value Value Type Package main Import " FMT " FUNC main () { var A Map [int] String #key is int, value is String fmt.Println (A) } # Results Map [] null #map type is nil
maps assignment and initialization values
Package main Import " FMT " FUNC main () { var b map [int] int = the make (map [int] int) # use map initialization b [. 1] = 100 # to b which set value, key value form b [2 ] = 50 fmt.Println (B) fmt.Println (B [ 1]) # 1 acquires a value of the key value to } # results Map [1: 100 2:50 ] 100
Take a non-existent key value what will happen? (Returns the value null values)
main Package Import " FMT " FUNC main () { var Map B [int] int = the make (Map [int] int) B [ . 1] = 100 B [ 2] = 50 fmt.Println (B [ . 5 ]) # go value 5 key value, whichever is less. Because the value is of type int and the printed 0 } # Results 0
We want to know in the end the map is not the existence of this key, you can judge by ok. If ok is true that the presence of the key, the corresponding value is the key value, and vice versa indicates key does not exist.
package main import "fmt" func main() { var b map[int]int=make(map[int]int) b[1]=100 b[2]=50 fmt.Println(b) fmt.Println(b[1]) #100 fmt.Println(b[5]) #0 v,ok:=b[1] if ok==true { #判断 fmt.Println(v) #100 } }
A second way to define and initialize
main Package Import " FMT " FUNC main () { var A = Map [int] {String. 1: "10", 2: "20 is" } # Key, in the form of value initialization value fmt.Println (A [ . 1 ]) # 10 } # results 10
Delete the map elements
Delete map
the key
syntax is [ the Delete (the Map, Key) ]. This function does not return a value.
main Package Import " FMT " FUNC main () { var A = Map [int] {String. 1: " 10 " , 2: " 100 " } Delete (A, 2 ) can only be deleted exists # Key Delete (A, . 3 ) # without this key will not delete fmt.Println (A) } # results map [1:10]
Get the length of the map
package main import "fmt" func main() { var a =map[int]string{1:"10",2:"100"} fmt.Println(len(a)) } #结果 2
Map is a reference type
map is a reference type. When the map is assigned to a new variable, they point to the same internal data structure. Therefore, changing one variable will affect other variables .
main Package Import " FMT " FUNC main () { var A = Map [int] {String. 1: " 10 " , 2: " 100 " } test2 (A) fmt.Println (A) } FUNC test2 (A Map [int ] String) { a [ . 1] = " 20 is " # test1 is a modification will affect the original a fmt.Println (a) } # results Map [1:20 2: 100 ] Map [ 1:20 2: 100 ]
Map for equality
can not be used between the map ==
operator judgment, ==
it can only be used to check whether the map nil
.
The method of determining whether two map is equal to compare two traverse map each element
Map cycle all the elements
Method 1: The for loop, this approach is not advocated
var a =map[int]string{1:"10",0:"100",10:"999"} for i:=0;i<len(a);i++{ fmt.Println(a[i]) }
Method 2: The way for range and
var a =map[int]string{1:"10",0:"100",10:"999"}
for k, i = range {a fmt.Println (a) fmt.Println (v) }
Added: Slice remove elements
The index taken, then placed in a new array
var a =[]int{1,2,3,4,5,6} a=append(a[0:2],a[3:]...) fmt.Println(a)