Article Directory
Foreword
Hash
Table is a clever and practical data structure is an unordered key/value
collection of pairs, wherein all of the key is different by a given key
can be retrieved in constant time complexity, update or delete the corresponding value
. Map is actually a Hash
list of references, you can quickly retrieve the data based on the key, the same key as the index value associated with the key point of. Have the opportunity to give you speak Map
the bottom of things, it teaches you how to use Map
the focus of this section is to remember one thing: Map
store unordered collection of key-value pairs.
Creation and initialization
Make use function
make
You can create a slice, it can also be used to create Map
. Rule is this:
m := make(map[keyType]valueType)
Let's try to create:
month := make(map[string]int)
month["January"] = 1
month["February"] = 2
month["March"] = 3
The first line of code, create a key
type string
, value
type int
of blank Map
month
, then to month
assign the three pairs.
Use literal
The above code, literal manner may be employed to achieve:
month := map[string]int{"January":1,"February":2,"March":3}
// 还可以写成这样
month := map[string]int{
"January":1,
"February":2,
"March":3,
}
Use literals can also create empty Map
, braces which are not assigned to it:
month := map[string]int{}
fmt.Println(month) // 输出:map[]
Free Map
, is not there nil
Map
? Promising course nil
of Map
:
var month map[string]int
fmt.Println(month == nil) // 输出:true
For nil
the map
can not access key-value pairs, otherwise it will error panic: assignment to entry in nil map
. You can use mentioned make
functions, to their initialization:
var month map[string]int
month = make(map[string]int)
month["January"] = 1
fmt.Println(month) // 输出:map[January:1]
Nature can think of, Map
the value is zero nil
, Map
that is, the underlying Hash
reference tables.
Map
The key
may be a built-in type, structure type may be, may be used as long as the ==
comparison operators, can be used as key
. Sections, functions, and structures of the type comprising sections, because of these types reference semantics, not as key
the use of these types of errors will result in compilation:
month := map[[]string]int{}
// 编译错误:invalid map key type []string
For Map
in value
it, there would be no restrictions on the type of course there is no reason to prevent users from using the slice as Map
values:
m := map[string][]int{}
slice := []int{1,2,3}
m["slice"] = slice
fmt.Println(m["slice"])
// 或者
slice := []int{1,2,3}
m := map[string][]int{"slice":slice}
fmt.Println(m["slice"])
First piece of code, create a key
type string
, a value of slice
type null Map
, then slice slice
assigned to the called slice
of key
. The second paragraph of the code is a shorthand version of the first paragraph of the code.
How to use the Map
Map
The use is very simple, similar to the array, the array is to use the index Map
to use key
to obtain or modify value
.
m := map[string]int{}
m["January"] = 1 // 赋值
fmt.Println(m) // 输出:map[January:1]
m["January"] = 10 //修改
fmt.Println(m) // 输出:map[January:10]
january := m["January"] // 获取value
fmt.Println(january) // 输出:10
When modify operation, if key
already exists, the new value will overwrite the old value, the above code has been manifested, so key
is not allowed to repeat.
Gets a nonexistent key
in value
time, will return zero values corresponding to the type, this time, we do not know is there is a zero value or key-value pairs for it does not exist. Fortunately, Map
it provides us with a method:
february,exists := m["February"]
fmt.Println(february,exists) // 输出:0 false
Get more value when a return value, the return value is the first value
, the second is the return value boolean
type variable indicating value
whether or not there. This gives us a judgment key
whether there is provides a great convenience.
Delete key to delete
Unlike Slice
, Go
provides us with a key to the delete function - delete
function.
Prototype:
m := map[string]int{
"January":1,
"February":2,
"March":3,
}
fmt.Println(m) // 输出:map[March:3 January:1 February:2]
delete(m,"January")
fmt.Println(m) // 输出:map[February:2 March:3]
When you delete a key-value pair does not exist, delete
the function does not complain, did not have any effect.
Traversal Map
Map
Can not use for
loop through, like an array, slice, can be used range
to traverse.
for key, value := range m {
fmt.Println(key, "=>", value)
}
Output:
February => 2
March => 3
January => 1
Blank operator can _
ignore the return key
or value
. When repeatedly executing code, you will find that the return value of the order is likely is not the same, that is to say Map
traversal is disordered.
len function
You can use len function returns the number of key-value pairs in the Map:
fmt.Println("len(m) =",len(m))
Map is a reference type
Map is a reference to the underlying data . The coding process, involves Map
copying, transfer functions between Map and the like. With Slice
similar Map
underlying data points will not happen copy
of.
m := map[string]int{
"January":1,
"February":2,
"March":3,
}
month := m
delete(month,"February")
fmt.Println(m)
fmt.Println(month)
Output:
map[January:1 March:3]
map[January:1 March:3]
The above code, Map
m
is assigned to month
, delete month
a key-value pair, m
also changed, indicating that Map
copying, m
and month
share the underlying data, wherein one of the data changes, the other will also change. Similarly, the transfer function between Map
when, in fact, passed a Map
reference to a copy of the underlying data will not be involved, if modified in the called function Map
, in the calling function will be perceived Map
changes.
What if I really want to copy Map
how to do?
month := map[string]int{}
m := map[string]int{
"January":1,
"February":2,
"March":3,
}
for key,value := range m{
month[key] = value
}
delete(month,"February")
fmt.Println(m)
fmt.Println(month)
Output:
map[January:1 February:2 March:3]
map[January:1 March:3]
The above code, we use range
the m
key-value pairs assigned to the cycle month
, and then remove month
one of the key-value pairs, it can be seen by the results of printing, m
it has not changed. This enables a true copy.