Created
February 18, 2019 16:23
-
-
Save cjgiridhar/241ff7df015177ddc7dc48c9d5abfe7c to your computer and use it in GitHub Desktop.
Nested Maps in Golang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
It's a great demo! Thanks your code.