Replace Duplicate Helpers with Go Generics
Write one type-safe function instead of many type-specific helpers using Go generics, the comparable constraint, and the standard library.
25 Oct 2025, 18:54 UTC

The problem: one helper per type
Imagine your service needs the smaller of two values in several places. Without generics, you might write a separate function for each type:
func MinInt(a, b int) int {
if a < b {
return a
}
return b
}
func MinFloat64(a, b float64) float64 {
if a < b {
return a
}
return b
}
func MinString(a, b string) string {
if a < b {
return a
}
return b
}
Every function repeats the same comparison logic. Adding support for int32 or []byte means another copy. The older alternative, interface{} with type assertions, moves type errors from compile time to runtime.
The fix: type parameters and comparable
Go 1.18 introduced type parameters, which let you write functions and types over a set of types while keeping compile-time type checking. A type parameter, conventionally T, is a placeholder that the caller fills with a concrete type such as int or string.
Not every type supports every operation. To use equality operators and map keys with a type parameter, apply the built-in comparable constraint. It guarantees == and != work, but it does not guarantee ordering operators such as <.
func Min[T comparable](a, b T) T {
if a < b {
return a
}
return b
}
That example compiles only for types that are both comparable and orderable. For a constraint that explicitly covers ordering, use cmp.Ordered from the standard library, available in Go 1.21 and later:
import "cmp"
func MinOrdered[T cmp.Ordered](a, b T) T {
if a < b {
return a
}
return b
}
Worked example: generic minimum helper
Create a file named min.go in a Go module directory:
package main
import (
"fmt"
"cmp"
)
func Min[T cmp.Ordered](a, b T) T {
if a < b {
return a
}
return b
}
func main() {
fmt.Println(Min(3, 7))
fmt.Println(Min(2.5, 1.2))
fmt.Println(Min("a", "z"))
}
Run these commands in the module directory:
go version
go build ./...
go run .
Where: your terminal, inside the Go module directory. Permissions: normal file access only. Expected: go version reports 1.21 or later; go build compiles without errors; go run prints the smaller value from each pair. Risks: on toolchains older than 1.18, type parameters are a syntax error; on 1.18 through 1.20, cmp.Ordered is unavailable.
Go infers T from the arguments, so you call Min(3, 7) rather than Min[int](3, 7). Supply explicit type arguments only when inference is ambiguous.
Standard library: slices, maps, and cmp
Go 1.21 added generic packages slices, maps, and cmp. Instead of writing manual loops, use functions such as slices.Index:
import "slices"
index := slices.Index([]int{1, 2, 3}, 2)
Inspect the signatures installed with your toolchain:
go doc slices
go doc cmp
Trade-offs and limitations
- Version requirement: generics require Go 1.18 or later;
slices,maps, andcmprequire 1.21 or later. - Compile time and binary size: instantiating many generic types with different concrete types can increase compile times and binary size.
- Readability: a single-use helper is sometimes clearer as a concrete function than a generic abstraction.
- Language limits: Go does not support self-referential generic types, and type parameters cannot appear directly in type switches or struct field tags.
Check your result
- Run
go versionand confirm the toolchain is at least 1.18, preferably 1.21 or later. - Compile the generic function with
go build ./.... - Inspect standard-library signatures with
go doc slicesandgo doc cmp. - If you replaced duplicated helpers, compare build times and binary size before and after using
go buildandls -lon the resulting binary.
Bottom line
Use Go generics when the same logic repeats across types and you want compile-time safety without interface{} assertions. Start with comparable for equality and map keys, reach for cmp.Ordered when ordering is required, and prefer slices and maps over hand-rolled loops. Measure compile time and binary size if you instantiate generics heavily, and keep single-use helpers concrete when they are easier to read.