"GoLang" Dictionary Map

mapIs an element of the unordered collection of elements referred to as a group value, the other group is a unique key index key. Uninitialized mapvalue nil. map Is a reference type, you can use the following statement:

var map1 map[keytype]valuetype
var map1 map[string]int

[keytype]And valuetypebetween Spaces are allowed, but gofmt spaces removed

When it was declared not need to know mapthe length mapis dynamic growth.

Uninitialized mapvalues are nil.

can be used can be any key ==or !=type of operation of comparison operators, such as string, int, float. Therefore arrays, slices, and not as a key structure (a structure containing an array of slices not as the key, comprising only the built-in types structthat can be used as a key), but the pointer and can interface type . If the use as the key structure may be provided Key()and Hash()methods, so that the key can be calculated only by domain number or string structure.

value can be any type; by using an empty interface type, we can store any value, but the use of this type as a value type needs to do first assertion.

Consideration map passed to the function is very small: 4 bytes in the 32-bit machines, 8 bytes on a 64-bit machine, regardless of how much data is actually stored. By looking at the map key value is very fast, much faster than linear look, but still read directly from the index of the array and slice slower than 100 times; so if you care about performance, then it is recommended to use slices to solve problem.

If key1 is map1 the key, so map1[key1]that the corresponding value key1, the same symbols as if the array index (array may be viewed as a simple form of map, key is an integer of from zero).

So v := map1[key1]key1 corresponding value can be assigned to v; key1 if not present in the map, then v is assigned to the null value type of map1.

Commonly used len(map1)ways to get the number pair map in this number are retractable, since map-pairs can be dynamically added and removed at runtime.

map is a reference type : Memory allocation method can make.

var map1 = make(map[keytype]valuetype, cap)

Or abbreviated as:

map1 := make(map[keytype]valuetype,cap)

Do not use new, always used maketo constructmap

Note that if you use the wrong new()assigned a reference to an object, you will get a pointer to a null reference , is equivalent to declare an uninitialized variable and taking its address :

mapCreated := new(map[string]float32)

Then when we call: mapCreated["key1"] = 4.5the time, the compiler will complain:

invalid operation: mapCreated["key1"] (index of type *map[string]float32).

The following are mapsome of the applications:

val, ok = map1[key1] // 第一个是key1的值,如果key1存在则ok == true,否则ok为false

delete(map1, key1) // 从map1中删除key1,如果 key1 不存在,该操作不会产生错误。

for key, value := range map1 {
    // 遍历所有键值对
}

for _, value := range map1 {
    //只关心value
}

for key := range map1 {
    // 值关心key
}

Note that map is not arranged in the order of the key, is not arranged according to the order value.

The default map is disordered, whether it is in accordance with the key still in accordance with the default value is not sorted.

If you want to map necessary for sorting the key (or value) to a copy of the slice, and then the sorting sections (with the sort packages), then slices can be used for-rangea method to print out all of the key and value.

In another case, that is the key exchange and value, this means the swap exchange key and value. If the map value type can be used as the key value, and all are unique, then the key can be reversed simply done by the following method.

package main
import "fmt"

var (
    barVal = map[string]int{"alpha": 34, "bravo": 56, "charlie": 23,
                            "delta": 87, "echo": 56, "foxtrot": 12,
                            "golf": 34, "hotel": 16, "indio": 87,
                            "juliet": 65, "kili": 43, "lima": 98}
)

func main() {
    invMap := make(map[int]string, len(barVal))
    for k, v := range barVal {
        invMap[v] = k
    }
    fmt.Println("inverted:")
    for k, v := range invMap {
        fmt.Printf("Key: %v, Value: %v / ", k, v)
    }
}

Guess you like

Origin www.cnblogs.com/ice-coder/p/12667929.html