Security with Go
上QQ阅读APP看书,第一时间看更新

Map

A map is a hash table or dictionary that stores key and value pairs. The key and value can be any data types, including maps themselves, creating multiple dimensions.

The order is not guaranteed. You can iterate over a map multiple times and it might be different. Additionally, maps are not concurrent safe. If you must share a map between threads, use a mutex.

Here are some example map usages:

package main

import (
"fmt"
"reflect"
)

func main() {
// Nil maps will cause runtime panic if used // without being initialized with make()
var intToStringMap map[int]string
var stringToIntMap map[string]int
fmt.Println(reflect.TypeOf(intToStringMap))
fmt.Println(reflect.TypeOf(stringToIntMap))

// Initialize a map using make
map1 := make(map[string]string)
map1["Key Example"] = "Value Example"
map1["Red"] = "FF0000"
fmt.Println(map1)

// Initialize a map with literal values
map2 := map[int]bool{
4: false,
6: false,
42: true,
}

// Access individual elements using the key
fmt.Println(map1["Red"])
fmt.Println(map2[42])
   // Use range to iterate through maps
for key, value := range map2 {
fmt.Printf("%d: %t\n", key, value)
}

}