官术网_书友最值得收藏!

  • Security with Go
  • John Daniel Leon
  • 168字
  • 2021-06-30 19:06:48

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)
}

}
主站蜘蛛池模板: 洛扎县| 临夏县| 唐山市| 垫江县| 萍乡市| 高青县| 江川县| 宜良县| 新和县| 垣曲县| 舞钢市| 蛟河市| 松溪县| 昔阳县| 闻喜县| 鲁甸县| 东阿县| 准格尔旗| 张家口市| 小金县| 大足县| 蒙城县| 红桥区| 镇原县| 遂平县| 宣武区| 密云县| 云和县| 西充县| 扎鲁特旗| 若尔盖县| 梧州市| 涟水县| 南溪县| 道孚县| 通城县| 嵩明县| 丹棱县| 连州市| 瑞丽市| 会昌县|