Appearance
Go: slices of structs and keyed slice literals
Status: accepted · Date: 2026-11-30
Step 4 of the Go plan lists keyed elements, slices of structs and xs == nil among the slice expressions still to do. This record adds the first two and explains why the third waits.
The decision
- Slices of structs.
[]Pfor a struct typePof the subset is now a type of the subset (Pure.elemTypeOf). It covers parameters, locals,[]P{P{…}, …},append,lenandps[i].f. The output isList P. Forps[i], the output's default element is the struct with each field's zero value (Pure.elemZero). The output reads it only when the index is out of range, and then the statement panics first. - Keyed elements.
[]T{2: a, b, 5: c}puts each element at its index: the key's, or one after the previous element's. The zero value ofTfills the gaps (Go specification, "Composite literals").Eval.slicePositionscomputes the indices andEval.placeAtlays the values out. The interpreter and the translator share both. The output is the list with the zeros written out.
Refused
- Keys that are not integer literals, or that do not grow. Go evaluates the elements in source order, and the output evaluates its list in index order, so the two orders agree only when the keys grow. Go accepts constant expressions as keys, and any order of distinct keys.
- Keys above 2^20, as for
make. - A keyed literal of structs. The gaps would need a struct's zero value in the interpreter as well, which is left for later.
- Elements without their type, such as
[]P1. Slices of slices came later (decision). xs == nilandxs != nil. The subset models a slice as the list of its elements, soniland an empty slice such as[]int{}ormake([]int, 0)have the same value. Go tells them apart. Supporting the comparison needs a slice value that records whether it isnil. That changes the interpreter'sVal.slice,append(which turnsnilinto a non-nil slice), and the output type, which would becomeOption (List T)or a structure. It is left for a later step.
This page is docs/decisions/2026-11-30-go-slice-literals.md at commit 5f1d031.
