Home »
Golang
Go Maps
Last Updated : May 27, 2025
In Go, a map
is a built-in data type that associates keys with values.
Declaring and Initializing a Map
You can declare and initialize a map using the make
function or with a map literal.
Example
This example demonstrates how to create and initialize a map using a literal:
package main
import "fmt"
func main() {
capitals := map[string]string{
"India": "New Delhi",
"France": "Paris",
"Japan": "Tokyo",
}
fmt.Println("Capital of India:", capitals["India"])
}
When executed, this program outputs:
Capital of India: New Delhi
Creating an Empty Map
The make
function is used to create an empty map and then add key-value pairs to it.
Example
This example creates a map and adds elements using key assignment:
package main
import "fmt"
func main() {
scores := make(map[string]int)
scores["Alice"] = 90
scores["Bob"] = 85
fmt.Println("Scores:", scores)
}
When executed, this program outputs:
Scores: map[Alice:90 Bob:85]
Accessing Map Elements
To access a value, use the key in square brackets. If the key does not exist, the zero value of the value type is returned.
Example
Here we access existing and non-existing keys in a map:
package main
import "fmt"
func main() {
ages := map[string]int{
"Rahul": 25,
"Simran": 28,
}
fmt.Println("Rahul's age:", ages["Rahul"])
fmt.Println("John's age:", ages["John"]) // Key doesn't exist
}
When executed, this program outputs:
Rahul's age: 25
John's age: 0
Checking if a Key Exists
To check if a key exists, use the second value returned by the map access expression.
Example
This example shows how to check for the presence of a key in a map:
package main
import "fmt"
func main() {
data := map[string]string{
"language": "Go",
}
value, exists := data["language"]
fmt.Println("Exists:", exists, "Value:", value)
value, exists = data["framework"]
fmt.Println("Exists:", exists, "Value:", value)
}
When executed, this program outputs:
Exists: true Value: Go
Exists: false Value:
Deleting Map Elements
You can delete a key-value pair from a map using the delete
function.
Example
This example shows how to delete an element from a map:
package main
import "fmt"
func main() {
countryCodes := map[string]string{
"US": "United States",
"IN": "India",
}
delete(countryCodes, "US")
fmt.Println("After deletion:", countryCodes)
}
When executed, this program outputs:
After deletion: map[IN:India]
Iterating Over a Map
You can use a for...range
loop to iterate over all key-value pairs in a map.
Example
This example demonstrates how to iterate over a map:
package main
import "fmt"
func main() {
colors := map[string]string{
"R": "Red",
"G": "Green",
"B": "Blue",
}
for key, value := range colors {
fmt.Printf("Key: %s, Value: %s\n", key, value)
}
}
When executed, this program outputs:
Key: R, Value: Red
Key: G, Value: Green
Key: B, Value: Blue
Go Maps Exercise
Select the correct option to test your understanding of maps in Go.
- Which of the following creates a new map?
- What will map[key] return if the key is not present?
- Which built-in function is used to remove an element from a map?
Advertisement
Advertisement