Public
Snippet $1655246 authored by Jesse

Example of using a map within a map in the Go language

Edited
map.go
/*\
 * File: map.go
 * Language: Go
 * Version: 0
 * Author: culb (nightfrog)
 * Contact: the01culb[at]gmail[dot]com
 *
 *  If this code is used in any way then
 *  be courteous and include all of my information and notes
\*/
 
/* Each Go program has a "main" starting point */
/* All other files in a project must have the same NAME. ie, package NAME */
/* main is the executed file */
package main
 
/* Import the fmt package for various I/O functions*/
import "fmt"
 
/* This doesn't need to be named main for an extry point like in C or C++ */
func main() {
 
    /* Intialize  map */
    humans := map[string]map[string]string{
        "earth": map[string]string{"nightfrog": "Park Street"},
    }
 
    /* Add to the map */
    humans["Pluto"] = map[string]string{"Corgan": "Oporto Madrid"}
 
    /* Change an existing inner map */
    humans["Pluto"]["Corgan"] = "Jux Terrace"
 
    /* Loops through the maps */
    for planet := range humans {
        for human := range humans[planet] {
            fmt.Printf("Planet %s, human %s, street %s\n", planet, human, humans[planet][human])
        }
    }
}