In our previous tutorial, we have explained how to handle Marshalling and Unmarshalling in Golang. In this tutorial, we will explain about Maps in Golang. Map in Golang is a data structure that provides an unordered collection of keys/values pairs. Golang maps are same like associative arrays, hash or dictionaries in other programming languages.
The maps are used to store and access values based on key. A key works like an index, pointing to the value associate with that key. A map data structure is used for fast lookup, retrieval and deletion of the data based on keys.
A map is implemented using hash table that provides faster lookup on the data element using keys. Maps are unordered and there are no fixed order in which the key/value pairs will be returned.
Also, read:
We will cover following in this tutorial to work with Maps in Golang.
- Create Map in Golang
- Add Item to Map in Golang
- Retrieve Item from Map in Golang
- Update Item to Map in Golang
- Delete Item from Map in Golang
- Truncate Map in Golang
- Merge Maps in Golang
1. Create Map in Golang
In Golang, maps are created using make() and map() function with curly brackets and have keys and values. It creates an instance of map data type with keys and values.
var employee = make(map[string]int)
Here in below example, variable employee is a map of string keys and int values. We have assigned employee salary and age keys with values.
package main
import "fmt"
func main() {
var employee = map[string]int{"salary": 1000, "age": 20}
fmt.Println(employee)
}
Now, when we run above program, we should see the following output:
map[age:20 salary:1000]
2. Adding Item to Map in Golang
Items can be added to map using a new index key and assigning value to key.
Here in below example, we are adding phone and pin to employee map object.
package main
import "fmt"
func main() {
var employee = make(map[string]int)
employee["phone"] = 123456789
employee["pin"] = 12345
fmt.Println(employee)
}
Now, when we run above program, we should see the following output:
map[phone:123456789 pin:12345]
3. Retrieve Item from Map in Golang
We can easily get the items from map by using it’s key. We need to pass key name inside map brackets to get the items.
Here in below example code, we will get the value of key Smith and Rose.
package main
import "fmt"
func main() {
var employee = map[string]int{"Smith": 30, "Rose": 25}
fmt.Println(employee["Smith"])
fmt.Println(employee["Rose"])
}
Now, when we run above program, we should see the following output:
30 25
4. Update Item to Map in Golang
We can update the value of a key by referring to its key in map brackets.
Here in below example code, we are updating value of key Smith from 30 to 40.
package main
import "fmt"
func main() {
var employee = map[string]int{"Smith": 30}
employee["Smith"] = 40
fmt.Println(employee["Smith"])
}
Now, when we run above program, we should see the output 40 instead of 30:
40
5. Delete Item from Map in Golang
There are a function delete() to delete an item from map. We need to pass map with key to delete.
In below example code, we are deleting item Rose from employee map.
package main
import "fmt"
func main() {
var employee = map[string]int{"Smith": 30, "Rose": 25}
delete(employee, "Rose")
fmt.Println(employee)
}
Now, when we run above program, we should see the following output after deleting an item:
map[Smith:30]
6. Merge Maps in Golang
We can merge the maps to an exsting maps by passing keys and values of a maps.
Here in below example, we will loop through Student maps and passed keys and value to employee to merge it.
package main
import "fmt"
func main() {
var employee = map[string]int{"Smith": 30, "Rose": 25}
var Student = map[string]int{"Goerge": 50, "William": 35}
for k, v := range Student {
employee[k] = v
}
fmt.Println(employee)
}
Now, when we run above program, we should see the following output after merging maps:
map[Goerge:50 Rose:25 Smith:30 William:35]
7. Truncate Map in Golang
We can easily empty the map or delete all items by redefining the map.
package main
import "fmt"
func main() {
var employee = map[string]int{"Smith": 30, "Rose": 25}
var employee = make(map[string]int)
fmt.Println(employee)
}
Now, when we run above program, we should see the following output after truncating map:
map[]
You may also like:
- Regular Expressions in Golang
- Creating REST API with Golang
- How to Delete Files in Golang
- Working with Structs in Golang
- Working with Date and Time in Golang
- How to Make HTTP Requests in Golang
- Working with Channels in Golang
- Concurrency with Goroutines in Golang
- Marshalling and Unmarshalling in Golang
- AWS S3 File Upload in Golang
- Write Data to CSV File using Golang
- How to Read CSV File using Golang
- Parsing JSON Data using Golang
- Read File Line by Line using Golang