To create a map of maps in Go, you can use the map data structure. A map of maps allows you to organize data hierarchically, where each key in the outer map points to another map.
We can create a map of maps in Go by using the map data structure. This example demonstrates creating a map where both the outer and inner maps use integers for keys and values.
For example,
outerMap
where each value is another map.package main
import "fmt"
func main() {
// Declare a map of maps
outerMap := make(map[int]map[int]int)
// Populate the inner maps
innerMap1 := map[int]int{1: 10, 2: 20, 3: 30}
innerMap2 := map[int]int{4: 40, 5: 50, 6: 60}
// Insert the inner maps into the outer map
outerMap[1] = innerMap1
outerMap[2] = innerMap2
// Print the resulting map of maps
fmt.Println("Map of maps with integer keys and values:")
for outerKey, innerMap := range outerMap {
fmt.Printf("Outer key: %d\n", outerKey)
for innerKey, value := range innerMap {
fmt.Printf(" Inner key: %d, value: %d\n", innerKey, value)
}
}
}
Map of maps with integer keys and values: Outer key: 1 Inner key: 1, value: 10 Inner key: 2, value: 20 Inner key: 3, value: 30 Outer key: 2 Inner key: 4, value: 40 Inner key: 5, value: 50 Inner key: 6, value: 60
We can create a map of maps in Go by using the map data structure. This example demonstrates creating a map where the outer map uses strings for keys and the inner maps use integers for keys and strings for values.
For example,
outerMap
where each value is another map with mixed key and value types.package main
import "fmt"
func main() {
// Declare a map of maps
outerMap := make(map[string]map[int]string)
// Populate the inner maps
innerMap1 := map[int]string{1: "one", 2: "two", 3: "three"}
innerMap2 := map[int]string{4: "four", 5: "five", 6: "six"}
// Insert the inner maps into the outer map
outerMap["first"] = innerMap1
outerMap["second"] = innerMap2
// Print the resulting map of maps
fmt.Println("Map of maps with mixed key and value types:")
for outerKey, innerMap := range outerMap {
fmt.Printf("Outer key: %s\n", outerKey)
for innerKey, value := range innerMap {
fmt.Printf(" Inner key: %d, value: %s\n", innerKey, value)
}
}
}
Map of maps with mixed key and value types: Outer key: first Inner key: 1, value: one Inner key: 2, value: two Inner key: 3, value: three Outer key: second Inner key: 4, value: four Inner key: 5, value: five Inner key: 6, value: six
We can create a map of maps in Go by using the map data structure. This example demonstrates creating a map where both the outer and inner maps use strings for keys and float64 for values.
For example,
outerMap
where each value is another map with strings for keys and float64 for values.package main
import "fmt"
func main() {
// Declare a map of maps
outerMap := make(map[string]map[string]float64)
// Populate the inner maps
innerMap1 := map[string]float64{"A": 1.1, "B": 2.2, "C": 3.3}
innerMap2 := map[string]float64{"D": 4.4, "E": 5.5, "F": 6.6}
// Insert the inner maps into the outer map
outerMap["group1"] = innerMap1
outerMap["group2"] = innerMap2
// Print the resulting map of maps
fmt.Println("Map of maps with string keys and float64 values:")
for outerKey, innerMap := range outerMap {
fmt.Printf("Outer key: %s\n", outerKey)
for innerKey, value := range innerMap {
fmt.Printf(" Inner key: %s, value: %.1f\n", innerKey, value)
}
}
}
Map of maps with string keys and float64 values: Outer key: group1 Inner key: A, value: 1.1 Inner key: B, value: 2.2 Inner key: C, value: 3.3 Outer key: group2 Inner key: D, value: 4.4 Inner key: E, value: 5.5 Inner key: F, value: 6.6
In this tutorial, we learned How to Create a Map of Maps in Go language with well detailed examples.