Type Parameters Syntax
Generic functions and types with [T any]
Type Parameters Syntax is a free Go Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Generics background
Before Go 1.18, code that worked on multiple types required either interface{}/any (losing type safety) or code generation. Generics add type parameters for compile-time polymorphism.
Generic function syntax
Declare type parameters in square brackets after the function name:
func Map[T, U any](s []T, f func(T) U) []U {
result := make([]U, len(s))
for i, v := range s {
result[i] = f(v)
}
return result
}
nums := Map([]int{1,2,3}, func(n int) string {
return strconv.Itoa(n)
})Type inference
The compiler infers type arguments from the function arguments in most cases. You can specify them explicitly if inference fails.
// Inferred:
nums := Map([]int{1,2,3}, strconv.Itoa)
// Explicit:
nums := Map[int, string]([]int{1,2,3}, strconv.Itoa)Generic types
Structs and other types can also have type parameters:
type Stack[T any] struct {
items []T
}
func (s *Stack[T]) Push(v T) { s.items = append(s.items, v) }
func (s *Stack[T]) Pop() (T, bool) {
if len(s.items) == 0 { var zero T; return zero, false }
n := len(s.items)-1
v := s.items[n]
s.items = s.items[:n]
return v, true
}Multiple type parameters
Functions and types can have multiple type parameters:
type Pair[K, V any] struct { Key K; Value V }
func Zip[K, V any](keys []K, vals []V) []Pair[K, V] {
result := make([]Pair[K, V], min(len(keys), len(vals)))
for i := range result {
result[i] = Pair[K, V]{keys[i], vals[i]}
}
return result
}Zero value of type parameter
Declare a variable of the zero value with var zero T. This is necessary when returning a "no value" result from a generic function.
func First[T any](s []T) (T, bool) {
if len(s) == 0 { var zero T; return zero, false }
return s[0], true
}Methods on generic types
Methods on a generic type use the type's parameters, not their own. A method cannot introduce additional type parameters.
type Pair[K, V any] struct{ K K; V V }
func (p Pair[K, V]) Swap() Pair[V, K] { return Pair[V, K]{p.V, p.K} }Instantiation
Using a generic type with a concrete type argument is called instantiation. It happens at compile time, not runtime — no boxing overhead.
s := Stack[int]{}
s.Push(42)Type aliases and generic types
You can create type aliases for instantiated generic types:
type IntStack = Stack[int]Limitations
Type parameters cannot be used with type switches or type assertions. Methods on generic types cannot have additional type parameters. Generics cannot be used on operator-overloaded types without constraints.
When to use generics
Use generics for truly type-agnostic algorithms (container types, functional helpers like Map/Filter/Reduce) where the alternative would be code duplication or unsafe casts.
Quick Check
Where are type parameters declared in a generic function?
Recap: Type Parameters Syntax
Key points:
- Type parameters in square brackets: func Foo[T any](...)
- Works on functions and types (structs, etc.)
- Compiler infers type args from arguments in most cases
- Zero value: var zero T; no method-level type parameters
Frequently asked questions
Is the “Type Parameters Syntax” lesson free?
Yes — the full text of “Type Parameters Syntax” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.
What will I learn in “Type Parameters Syntax”?
Generic functions and types with [T any] You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Go Academy?
No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Type Parameters Syntax” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Go Academy lesson?
Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.