Maps 2
package main import "fmt" /* * This map pgm an example swiped from Doxsey's book */ func main() { // Here we define a map of map of string. Notice that elements := map[string]map[string]string{ "H": map[string]string{ "name":"Hydrogen", "state":"gas", }, "He": map[string]string{ "name":"Helium", "state":"gas", }, "Li": map[string]string{ "name":"Lithium", "state":"solid", }, "Be": map[string]string{ "name":"Beryllium", "state":"solid", }, "B": map[string]string{ "name":"Boron", "state":"solid", }, "C": map[string]string{ "name":"Carbon", "state":"solid", }, "N": map[string]string{ "name":"Nitrogen", "state":"gas", }, "O": map[string]string{ "name":"Oxygen", "state":"gas", }, "F": map[string]string{ "name":"Fluorine", "state":"gas", }, "Ne": map[string]string{ "name":"Neon", "state":"gas", }, } for true { // Prompt and read from the line. fmt.Print("Element: ") var abbr string cnt, _ := fmt.Scanln(&abbr) if cnt != 1 { break } // This fetches the array element, and a boolean // to tell if it was present. Report data about the // element, or error. descr, ok := elements[abbr] if ok { fmt.Printf("%s: state %s\n", descr["name"], descr["state"]) } else { fmt.Printf("Unknown element symbol %s\n", abbr) } } }