github.com/aykevl/tinygo@v0.5.0/testdata/map.go (about)

     1  package main
     2  
     3  var testmap1 = map[string]int{"data": 3}
     4  var testmap2 = map[string]int{
     5  	"one":    1,
     6  	"two":    2,
     7  	"three":  3,
     8  	"four":   4,
     9  	"five":   5,
    10  	"six":    6,
    11  	"seven":  7,
    12  	"eight":  8,
    13  	"nine":   9,
    14  	"ten":    10,
    15  	"eleven": 11,
    16  	"twelve": 12,
    17  }
    18  
    19  type ArrayKey [4]byte
    20  
    21  var testMapArrayKey = map[ArrayKey]int{
    22  	ArrayKey([4]byte{1, 2, 3, 4}): 1234,
    23  	ArrayKey([4]byte{4, 3, 2, 1}): 4321,
    24  }
    25  var testmapIntInt = map[int]int{1: 1, 2: 4, 3: 9}
    26  
    27  func main() {
    28  	m := map[string]int{"answer": 42, "foo": 3}
    29  	readMap(m, "answer")
    30  	readMap(testmap1, "data")
    31  	readMap(testmap2, "three")
    32  	readMap(testmap2, "ten")
    33  	delete(testmap2, "six")
    34  	readMap(testmap2, "seven")
    35  	lookup(testmap2, "eight")
    36  	lookup(testmap2, "nokey")
    37  
    38  	// operations on nil maps
    39  	var nilmap map[string]int
    40  	println(m == nil, m != nil, len(m))
    41  	println(nilmap == nil, nilmap != nil, len(nilmap))
    42  	println(testmapIntInt[2])
    43  	testmapIntInt[2] = 42
    44  	println(testmapIntInt[2])
    45  
    46  	arrKey := ArrayKey([4]byte{4, 3, 2, 1})
    47  	println(testMapArrayKey[arrKey])
    48  	testMapArrayKey[arrKey] = 5555
    49  	println(testMapArrayKey[arrKey])
    50  }
    51  
    52  func readMap(m map[string]int, key string) {
    53  	println("map length:", len(m))
    54  	println("map read:", key, "=", m[key])
    55  	for k, v := range m {
    56  		println(" ", k, "=", v)
    57  	}
    58  }
    59  func lookup(m map[string]int, key string) {
    60  	value, ok := m[key]
    61  	println("lookup with comma-ok:", key, value, ok)
    62  }