Maps
A map is a data structure that provides you with an unordered collection of key/value pairs (maps are also sometimes called associative arrays in Php, hash tables in Java, or dictionaries in Python). Maps are used to look up a value by its associated key. You store values into the map based on a key. The strength of a map is its ability to retrieve data quickly based on the key. A key works like an index, pointing to the value you associate with that key.
A map is implemented using a hash table, which is providing faster lookups on the data element and you can easily retrieve a value by providing the key. maps are unordered collections, and there's no way to predict the order in which the key/value pairs will be returned. Every iteration over a map could return a different order.
Map declaration in golang goes like this:
make[Key-type] [Value-type]
The Key-type specifies the type of a value that will be used to index the stored elements(Value-type) of the map. Map keys can be of any type numeric, string, Boolean, pointers, arrays, struct, and interface.
Maps Examples
1) Empty Map declaration
package main import "fmt" var employee = map[string]int{} func main() { fmt.Println(employee) }
Sample output of above program, shown as below.
map[]
C:\golang\maps>
Map employee created having string as key-type and int as value-type
2) Map initialization
The literal mapped values are specified using a colon-separated pair of key and value as shown in below example.
package main import "fmt" var employee = map[string]int{"Mark":10,"Sandy":20} func main() { fmt.Println(employee) }
Sample output of above program, shown as below.
map[Mark:10 Sandy:20]
C:\golang\maps>
The type of each key and value pair must match that of the declared elements in the map.
3) Creating Map using the make function
A map value can also be initialized using the make function.
package main import "fmt" func main() { var employee = make(map[string]int) employee["Mark"] = 10 employee["Sandy"] = 20 fmt.Println(employee) employeeList := make(map[string]int) employeeList["Mark"] = 10 employeeList["Sandy"] = 20 fmt.Println(employeeList) }
Sample output of above program, shown as below.
map[Mark:10 Sandy:20]
map[Sandy:20 Mark:10]
C:\golang\maps>
The make function takes as argument the type of the map and it returns an initialized map.
4) Find length of Map
The built-in len() function returns the number of elements in a map.
package main import "fmt" func main() { var employee = make(map[string]int) employee["Mark"] = 10 employee["Sandy"] = 20 // Empty Map employeeList := make(map[string]int) fmt.Println(len(employee)) // 2 fmt.Println(len(employeeList)) // 0 }
Sample output of above program, shown as below.
2
0
C:\golang\maps>
The len function will return zero for an uninitialized map.
5) Delete element from a Map
The built-in delete function deletes an element from a given map associated with the provided key.
package main import "fmt" func main() { var employee = make(map[string]int) employee["Mark"] = 10 employee["Sandy"] = 20 employee["Rocky"] = 30 employee["Josef"] = 40 fmt.Println(employee) delete(employee,"Mark") fmt.Println(employee) }
Sample output of above program, shown as below.
map[Rocky:30 Josef:40 Mark:10 Sandy:20]
map[Josef:40 Sandy:20 Rocky:30]
C:\golang\maps>
In above example delete function used to delete first element from employee map by passing key Mark as second argument in delete function.
6) Adding and Editing elements in Map
package main import "fmt" func main() { var employee = map[string]int{"Mark":10,"Sandy":20} fmt.Println(employee) // Initial Map employee["Rocky"] = 30 // Add element employee["Josef"] = 40 employee["Mark"] = 50 // Edit element fmt.Println(employee) }
Sample output of above program, shown as below.
map[Mark:10 Sandy:20]
map[Mark:50 Sandy:20 Rocky:30 Josef:40]
C:\golang\maps>
2 elements added and 1 edited in employee map after initialization.
7) for range loop to iterate over a Map
The for…range loop statement can be used to fetch the index and element of a map.
package main import "fmt" func main() { var employee = map[string]int{"Mark": 10, "Sandy": 20, "Rocky": 30, "Rajiv": 40, "Kate": 50} for key, element := range employee { fmt.Println("Key:", key, "=>", "Element:", element) } }
Sample output of above program, shown as below.
Key: Rocky => Element: 30
Key: Rajiv => Element: 40
Key: Kate => Element: 50
Key: Mark => Element: 10
Key: Sandy => Element: 20
C:\golang\maps>
Each iteration returns a key and its correlated element content.