Small slices and maps can be represented as JSON arrays and objects.

collection encoding JSON arrays and objects map naturally to Go slices and maps when the values are small and deterministic.

Encode Collections

apples
encode_collections.go
Replay: real traced execution (multi-file project)
package main

import (
	"fmt"
)

func main() {
	var apples = 3
	counts := map[string]int{
		"apples":  apples,
		"oranges": 2,
	}

	total := counts["apples"] + counts["oranges"]
	text := fmt.Sprintf(`{"apples":%d,"oranges":%d}`, counts["apples"], counts["oranges"])
	fmt.Println("apples=", counts["apples"])
	fmt.Println("total=", total)
	fmt.Println("json=", text)
}
package main

import (
	"fmt"
)

func main() {
	var apples = 1
	counts := map[string]int{
		"apples":  apples,
		"oranges": 2,
	}

	total := counts["apples"] + counts["oranges"]
	text := fmt.Sprintf(`{"apples":%d,"oranges":%d}`, counts["apples"], counts["oranges"])
	fmt.Println("apples=", counts["apples"])
	fmt.Println("total=", total)
	fmt.Println("json=", text)
}
package main

import (
	"fmt"
)

func main() {
	var apples = 5
	counts := map[string]int{
		"apples":  apples,
		"oranges": 2,
	}

	total := counts["apples"] + counts["oranges"]
	text := fmt.Sprintf(`{"apples":%d,"oranges":%d}`, counts["apples"], counts["oranges"])
	fmt.Println("apples=", counts["apples"])
	fmt.Println("total=", total)
	fmt.Println("json=", text)
}
  1. apples ← 3, counts ← map[string]int{"apples":3, "oranges":2}, total ← 5

    7func main() {8  var apples→ 3 = 3 //@apples=1, 59  counts→ map[string]int{"apples":3, "oranges":2} := map[string]int{10    "apples":  apples3,11    "oranges": 2,12  }1314  total→ 5 := counts["apples"]3 + counts["oranges"]215  text→ "{\"apples\":3,\"oranges\":2}" := fmt.Sprintf(`{"apples":%d,"oranges":%d}`, counts["apples"]3, counts["oranges"]2)16  fmt.Println("apples=", counts["apples"]3)17  fmt.Println("total=", total5)18  fmt.Println("json=", text"{\"apples\":3,\"oranges\":2}")19}
    outputapples= 3
    total= 5
    json= {"apples":3,"oranges":2}
  1. apples ← 1, counts ← map[string]int{"apples":1, "oranges":2}, total ← 3

    7func main() {8  var apples→ 1 = 19  counts→ map[string]int{"apples":1, "oranges":2} := map[string]int{10    "apples":  apples1,11    "oranges": 2,12  }1314  total→ 3 := counts["apples"]1 + counts["oranges"]215  text→ "{\"apples\":1,\"oranges\":2}" := fmt.Sprintf(`{"apples":%d,"oranges":%d}`, counts["apples"]1, counts["oranges"]2)16  fmt.Println("apples=", counts["apples"]1)17  fmt.Println("total=", total3)18  fmt.Println("json=", text"{\"apples\":1,\"oranges\":2}")19}
    outputapples= 1
    total= 3
    json= {"apples":1,"oranges":2}
  1. apples ← 5, counts ← map[string]int{"apples":5, "oranges":2}, total ← 7

    7func main() {8  var apples→ 5 = 59  counts→ map[string]int{"apples":5, "oranges":2} := map[string]int{10    "apples":  apples5,11    "oranges": 2,12  }1314  total→ 7 := counts["apples"]5 + counts["oranges"]215  text→ "{\"apples\":5,\"oranges\":2}" := fmt.Sprintf(`{"apples":%d,"oranges":%d}`, counts["apples"]5, counts["oranges"]2)16  fmt.Println("apples=", counts["apples"]5)17  fmt.Println("total=", total7)18  fmt.Println("json=", text"{\"apples\":5,\"oranges\":2}")19}
    outputapples= 5
    total= 7
    json= {"apples":5,"oranges":2}