skip to content
Alvin Lucillo

Generic map type

/ 1 min read

Go requires map keys to be comparable (e.g., not slice, map, functions), so to define a generic map type, you need to use comparable constraint. any won’t work here as shown by the compile-time error below.

type S[T comparable] map[T]struct{}

// type S[T any] map[T]struct{} // invalid map key type T (missing comparable constraint)