Go

What does the built-in clear() do for slices vs maps?

Question 65MediumGo 1.22 to 1.25

clear (Go 1.21) has different semantics depending on the type:

  • Map: deletes every entry, so len becomes 0. It is the only way to remove NaN keys, and it is faster than looping over delete.
  • Slice: sets every element up to len(s) to its zero value. len and cap do not change. It does not "empty" the slice.
  • Type parameter: the type set must be all maps or all slices.
s := []int{1, 2, 3}
clear(s)
fmt.Println(s, len(s)) // [0 0 0] 3

m := map[string]int{"a": 1, "b": 2}
clear(m)
fmt.Println(m, len(m)) // map[] 0

// Reuse a buffer: truncate + zero (zeroing matters for pointer elements / secrets)
buf := []*Node{ /* ... */ }
clear(buf)      // drop references so the GC can free the Nodes
buf = buf[:0]   // now empty, capacity retained

// Only affects [0:len], not the hidden tail up to cap
t := make([]int, 2, 4)
t = append(t, 7, 8)[:2]
clear(t)
fmt.Println(t[:4]) // [0 0 7 8]

Gotcha: people coming from other languages expect clear(s) to act like s = s[:0]. In Go you need both lines. The compiler turns for i := range s { s[i] = zero } into a memclr call, and clear makes that intent explicit.

More on Arrays, Slices, Maps & Strings

All 37 Arrays, Slices, Maps & Strings questions