Go Generics Laboratory

Trace identity, preserve first-seen order, and retain the caller's named slice type.

1 / 5

Three type parameters, three jobs

S ~[]T: caller's slice type. T any: arbitrary row value. K comparable: map key. A row can contain a slice and still deduplicate by an integer ID.

2 / 5

Trace first representatives

(4,Ada), (2,Lin), (4,Bea), (3,Kai) by ID → (4,Ada), (2,Lin), (3,Kai).

Map membership decides whether to append; map iteration must not determine output order.

3 / 5

API boundaries

Empty input or nil key function returns nil S. Returning []T instead of S loses the defined slice return type. A copied struct may still share pointer-backed data: generics do not deep-copy it.

4 / 5

Transfer

Dedupe the same rows by first letter of Name, then add (9,Ari). Explain the changed output before implementing. Design last-value retention at first-key positions as a separate variation.

5 / 5
←/→ or click edges to navigate · ? help · N notes · F fullscreen