Map
A map is a built-in data structure that is used to store a collection of unordered key-value pairs that can be the same or different data type. It is Go’s implementation of a hash table, which allows for efficient access, insertion, and deletion.
Syntax
Initializing an empty map
An empty map can be created using the create()
function and assigning it to a variable.
variable_name := create(map[key_data_type]value_data_type)
Initializing a map with existing key-value pairs
A map can also be initialized with key-values pairs by using a map literal.
map_name := map[key_data_type]value_data_type{key-1: value-1...}
Accessing Items
A value in the map can be accessed using its corresponding key value by putting the key in brackets.
map_name[key_value]
The value that results from the code above can also be stored in a variable for later use.
variable_name := map_name[key_value]
If the key does not exist, then a null
value will be returned based on the data type of the values in the map.
Adding Items
Add new key-value pairs into the map by setting the new key value to a new value.
map_name[new_key] = [new_value]
Examples
Create empty map
The following code demonstrates how to create and print an empty map.
package mainimport "fmt"func main() {// Create a empty map called emptyMapemptyMap := make(map[string]int)// Print mapfmt.Println(emptyMap)}
This will output:
map[]
Initialize a map with key-value pairs
The following code demonstrates how to initialize and print a map already containing key-value-pairs.
package mainimport "fmt"func main() {// Initialize map gradebook with valuesgradebook := map[string]float32{"John": 85.2, "Ana": 95.6}// Print map gradebookfmt.Println(gradebook)}
This will output:
map[John:85.2 Ana:95.6]
Access a key-value pair in the map
The following code will print the corresponding value for the key "John"
.
package mainimport "fmt"func main() {// Initialize map gradebook with valuesgradebook := map[string]float32{"John": 85.2, "Ana": 95.6}// Print the value with key "John"fmt.Println(gradebook["John"])}
This will output:
85.2
Access and store a key-value pair in a variable
The following code will access the value in a map corresponding to the key of “Ana”. It will store the result in a variable called anaScore
and print it.
package mainimport "fmt"func main() {// Initialize map gradebook with valuesgradebook := map[string]float32{"John": 85.2, "Ana": 95.6}// Store the value that has a key of "Ana" in anaScoreanaScore := gradebook["Ana"]fmt.Println(anaScore)}
This will output:
95.6
Accessing a key that does not exist
The following code demonstrates what is returned for a non-existent key. The map maps a string to a float.
package mainimport "fmt"func main() {// Initialize map gradebook with valuesgradebook := map[string]float32{"John": 85.2, "Ana": 95.6}// Store the value that has a key of "John" in johnScorejohnScore := gradebook["David"]// Since "David" does not exist in the map, 0 will be printedfmt.Println(johnScore)}
This will output:
0
Adding a new key-value pair into a map
The following code will first print an initialized map. It will then add two more key-value pairs into the map and print the map.
package mainimport "fmt"func main() {// Initialize map gradebook with valuesgradebook := map[string]float32{"John": 85.2, "Ana": 95.6}// Print the initialized mapfmt.Println(gradebook)// Add more key-value pairsgradebook["George"] = 76.4gradebook["Emma"] = 90// Print the map againfmt.Println(gradebook)}
This will output:
map[Ana:95.6 John:85.2]map[Ana:95.6 Emma:90 George:76.4 John:85.2]
Codebyte Example
The following code will summarize all the initializations and operations for a map in Go that has been shown in this document.
package mainimport "fmt"func main() {// Create empty map called mm := make(map[string]float32)// Print empty map mfmt.Println("Empty map m: ", m)// Add values to mm["Alex"] = 92.5m["Amanda"] = 85.2// Print map mfmt.Println("m with 2 added key-value pairs: ", m)// Create map called gradebook with values that has key data type as string and value data type as doublegradebook:= map[string]float32{"John" : 85.2, "Ana" : 95.6}// Output map gradebookfmt.Println("map gradebook: ", gradebook)// Access non-existing key gets zero valuefmt.Println("grade of Bob who is not in map gradebook: ", gradebook["Bob"])// Store the value of the key "Ana" into variableanaGrade := gradebook["Ana"]// Print the variablefmt.Println("Ana grade: ", anaGrade)// Add new key-value pairs into gradebookgradebook["Bob"] = 100gradebook["Elizabeth"] = 88.6// Output map gradebook with new valuesfmt.Println("map gradebook with 2 more values: ", gradebook)}
This will output:
Empty map m: map[]m with 2 added key-value pairs: map[Alex:92.5 Amanda:85.2]map gradebook: map[Ana:95.6 John:85.2]grade of Bob who does not exist within map gradebook: 0Ana grade: 95.6map gradebook with 2 more values: map[Ana:95.6 Bob:100 Elizabeth:88.6 John:85.2]