main.mx raw

   1  // A bare `{...}` in map key or value position takes its type from the map:
   2  // map[string]map[string]string{"en": {"k": "v"}} must hold a real inner map,
   3  // map[string][]string{"a": {"x", "y"}} a two-element slice, and a struct value
   4  // a built struct. Stage4 built none of them: buildExpr on an untyped
   5  // composite literal returned nil, the entry was dropped without a word, and
   6  // the program read an empty map. Map-index field selection (`m[k].Field`) hit
   7  // the same hole from the other side - the field select returned nil and the
   8  // enclosing call failed arg-count checking.
   9  package main
  10  
  11  import "fmt"
  12  
  13  type pt struct {
  14  	x string
  15  	y string
  16  }
  17  
  18  func main() {
  19  	nested := map[string]map[string]string{
  20  		"en": {"login": "EN LOGIN"},
  21  		"ja": {"login": "JA LOGIN"},
  22  	}
  23  	slices := map[string][]string{"a": {"one", "two"}}
  24  	structs := map[string]pt{"a": {x: "one", y: "two"}}
  25  	arrays := map[string][2]string{"a": {"first", "second"}}
  26  	ptrs := map[string]*pt{"a": &pt{x: "five", y: "six"}}
  27  
  28  	fmt.Println("nested en = " | nested["en"]["login"])
  29  	fmt.Println("nested ja = " | nested["ja"]["login"])
  30  	fmt.Println("slice = " | slices["a"][0] | "," | slices["a"][1])
  31  	fmt.Println("struct = " | structs["a"].x | "," | structs["a"].y)
  32  	fmt.Println("array = " | arrays["a"][0] | "," | arrays["a"][1])
  33  	fmt.Println("ptr = " | ptrs["a"].x | "," | ptrs["a"].y)
  34  }
  35