Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Nested Maps in Golang
package main
import "fmt"
func main() {
// shoppingList is a map that has a map inside it
shoppingList := make(map[string]map[string]int)
// veggies key points to veggiesMap
veggiesMap := map[string]int{"onion": 2, "orka": 3}
shoppingList["veggies"] = veggiesMap
// fruits key points to fruitsMap
fruitsMap := map[string]int{"banana": 12, "apples": 5, "oranges": 3}
shoppingList["fruits"] = fruitsMap
// Returns Shopping list categories:
// Category: veggies
// Category Details: map[onion:2 orka:3]
// Category: fruits
// Category Details: map[banana:12 apples:5 oranges:3]
fmt.Println("Shopping list categories:")
for key := range shoppingList {
fmt.Println("Category:", key)
fmt.Println("Category Details:", shoppingList[key])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment