0Pricing
Go Academy · Lesson

Generics in Practice: Pitfalls

When to use generics and common mistakes

Generics in Practice: Pitfalls is a free Go Academy lesson on CoddyKit — lesson 4 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.

Over-genericising

Not every function needs to be generic. If there is only one concrete type ever used, a generic adds complexity without benefit. Prefer concrete types and refactor to generics when duplication appears.

Type assertion inside generics

You cannot type-assert or type-switch inside a generic function on the type parameter itself — generics are not templates. Use interfaces or the reflect package instead.

// WRONG:
func Foo[T any](v T) {
    if x, ok := v.(int); ok { // compile error
    }
}

Comparable does not mean Ordered

comparable supports == only. To use <, >, you need cmp.Ordered or a custom constraint. A common mistake is assuming comparable covers ordering.

Method cannot introduce type parameters

Methods on generic types reuse the type's parameters but cannot add new ones. If a method needs an additional type parameter, use a top-level function instead.

// WRONG:
func (s *Stack[T]) MapTo[U any]() []U // compile error

Nil and zero values

For pointer type parameters, the zero value is nil. For value types, it is the type's zero. Always handle the zero case rather than assuming the value is non-nil.

Interface method sets and generics

A generic function constrained to an interface can only call methods declared in that interface. Adding methods to a concrete type does not make them available through the constraint.

Instantiation bloat (GC shapes)

Go shares implementations for types with the same GC shape (e.g., all pointer types share one implementation). Primitive types each get their own — but Go's approach keeps binary sizes reasonable.

Any is not interface{}

any is an alias for interface{}, but a generic function F[T any] is not the same as F(v interface{}) — in the generic, T is the concrete type at the call site, preserving type information.

Recursive generic types

Go does not support recursive type parameter definitions like type Tree[T Tree[T]]. Use composition instead:

type TreeNode[T any] struct {
    Val   T
    Left  *TreeNode[T]
    Right *TreeNode[T]
}

Pre-instantiated helpers

For performance-sensitive code, pre-instantiate frequently used generic functions as top-level functions to avoid repeated type inference overhead (though the Go compiler handles this well).

Testing generic code

Test generic functions with multiple instantiations: one for each distinct constraint (int, string, a custom type). This catches constraint-specific edge cases.

func TestMap(t *testing.T) {
    got := Map([]int{1,2}, double)
    // test with strings too
    gotS := Map([]string{"a","b"}, strings.ToUpper)
}

Quick Check

Why can you NOT type-switch on a generic type parameter T inside a generic function?

Recap: Generics Pitfalls

Key points:

  • Don't over-genericise; concrete types are simpler when used once
  • No type switch/assert on T inside generic functions
  • comparable ≠ ordered; use cmp.Ordered for < support
  • Methods cannot introduce new type parameters

Frequently asked questions

Is the “Generics in Practice: Pitfalls” lesson free?

Yes — the full text of “Generics in Practice: Pitfalls” 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 “Generics in Practice: Pitfalls”?

When to use generics and common mistakes 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 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Generics in Practice: Pitfalls” 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.

All lessons in this course

  1. Type Parameters Syntax
  2. Constraints: comparable and interfaces
  3. Generic Data Structures
  4. Generics in Practice: Pitfalls
← Back to Go Academy