map
Maps:
A map is a collection of key-value pairs, where the keys are unique and the values can be of any type. Maps can be accessed using the key, and they are not ordered.
In Go, map syntax looks like this:
var mapName map[keyType]valueType
For example, to create a map of strings to integers:
var stringToInt map[string]int
To create a map and initialize it with values, you can use the following syntax:
var stringToInt = map[string]int{
"one": 1,
"two": 2,
"three": 3,
}
Operations:
- Accessing a value: map[key]
- Setting the value of a key: map[key] = value
- Iterating through the keys and values: for key, value := range map
- Removing a key-value pair: delete(map, key)