In Go there is no difference: "generic function" and "type-parameterized function" are two names for the same feature. A generic function declares one or more type parameters in square brackets, such as func Map[T, U any](...), and works for any type that satisfies the constraints. Generics were added in Go 1.18, and the language specification uses the term "type parameters".
The real distinctions worth knowing are between generic functions and functions that use interface{} or any, between type parameters and ordinary parameters, and between constraints. This article covers those.
Type parameters go in square brackets between the function name and the ordinary parameters. Each has a constraint that limits which types are allowed.
T and U are type parameters, and any is the constraint meaning no restriction. At the call site the compiler infers T as string and U as int, so no explicit type list is needed. You can still write it out: Map[string, int](...).
A type parameter can only use operations that every type in its constraint supports. For ordering you use cmp.Ordered, available in the standard library since Go 1.21, and for equality comparable.
The ~int form means "any type whose underlying type is int", so named types such as type Age int are accepted too.
Before generics, a function that worked on many types took interface{} (now written any) and used type assertions. That pushes type errors to run time and boxes values.
| Approach | Type safety | Return type | Runtime cost |
|---|---|---|---|
func Max(a, b any) any | Checked at run time | any, needs assertion | Boxing and assertion |
Generic func Max[T cmp.Ordered](a, b T) T | Checked at compile time | T | No boxing for the value itself |
| Interface with methods | Compile time, by behavior | Interface or concrete | Dynamic dispatch |
Use an interface when types share behavior (methods). Use a type parameter when the algorithm is the same and the types only differ in data, as in containers and slice helpers.
Types can have type parameters as well, but methods cannot declare additional ones; they can only use the receiver's parameters.
The compiler infers type arguments from the ordinary arguments, but it cannot infer a type that appears only in the result. In that case you supply the type list yourself, and you can supply just the leading parameters.
Zero has no argument to infer T from, so Zero[int]() is required. In Convert, From is inferred from the argument and To is given explicitly.
slices, maps and cmp packages before writing your own helpers.No, they are the same feature. The Go specification calls them functions with type parameters.
Go 1.18, released in March 2022. The cmp package and built-in min, max and clear came in Go 1.21.
No. Methods can use only the type parameters declared on the receiver type.
Usually not. The compiler uses a form of stenciling by GC shape, so performance is close to hand-written code, but measure hot paths.
Generic functions and type-parameterized functions are the same thing in Go. Declare type parameters in square brackets, choose a constraint such as any, comparable or cmp.Ordered, and let type inference fill in the types. Prefer generics over any for compile-time safety, and interfaces when you want to share behavior.