go-data-idioms
Installation
SKILL.md
Go Data Idioms
Slices are (ptr, len, cap) headers over a shared backing array; aliasing is the default. defer evaluates args at the defer site, not at return. Design types so the zero value is usable.
new vs make
| Call | Returns | For |
|---|---|---|
new(T) |
*T, zeroed |
structs, ints, arrays |
make(T, …) |
T, initialized |
slices, maps, channels |
new([]int) yields *[]int pointing to a nil slice — rarely useful. Use make([]int, n).
Slices: aliasing and append
s := []int{1, 2, 3, 4}
t := s[1:3] // shares backing; t[0]=9 mutates s[1]
u := append(s, 5) // may or may not reallocate