Skip to content

Instantly share code, notes, and snippets.

@cjgiridhar
Created February 18, 2019 16:23
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cjgiridhar/241ff7df015177ddc7dc48c9d5abfe7c to your computer and use it in GitHub Desktop.
Save cjgiridhar/241ff7df015177ddc7dc48c9d5abfe7c to your computer and use it in GitHub Desktop.
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])
}
}
@stone2014
Copy link

It's a great demo! Thanks your code.

@Akron99
Copy link

Akron99 commented Sep 8, 2023

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment