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

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

}
主站蜘蛛池模板: 忻城县| 白玉县| 莲花县| 邯郸县| 和静县| 双峰县| 壶关县| 安阳县| 兴和县| 广东省| 民县| 鲁山县| 顺平县| 涟水县| 吕梁市| 宁化县| 红安县| 瑞安市| 兴国县| 龙南县| 栖霞市| 四平市| 常德市| 广西| 湘潭县| 天水市| 五峰| 景德镇市| 双柏县| 沾益县| 西充县| 石泉县| 韩城市| 临猗县| 清水县| 乌鲁木齐县| 阳西县| 陕西省| 镇江市| 平原县| 出国|