Backend Development 23 min read

Understanding Generics in Go: History, Concepts, and Practical Examples

The article surveys Go's generics—from their historical origins and the generic dilemma of balancing productivity, compile speed, and runtime performance—to practical implementation using type parameters, constraints, and the built‑in comparable interface, illustrating common patterns such as generic arithmetic, slices, maps, and utility functions.

Tencent Cloud Developer
Tencent Cloud Developer
Tencent Cloud Developer
Understanding Generics in Go: History, Concepts, and Practical Examples

This article provides a comprehensive overview of generics in the Go programming language, covering their historical background, conceptual foundations, design challenges, and practical usage in Go 1.17 and later versions.

What are generics? Generics allow writing code that can operate on values of any type. The article distinguishes two kinds of polymorphism: ad‑hoc polymorphism (e.g., function overloading) and parametric polymorphism , which is the essence of generics.

Generic dilemma – Adding generics forces language designers to trade off between programmer productivity, compilation speed, and runtime performance. The article cites Russ Cox's "generic dilemma" and discusses how languages like C, C++, and Java have handled generics differently.

Implementation approaches in Go – Several techniques are presented, including using type parameters on functions and types, defining constraints with interfaces, and leveraging the built‑in comparable constraint.

Below are representative code snippets (preserved verbatim):

func Add(a, b int) int { return a + b }
func Add(a, b float64) float64 { return a + b // note: Go does not allow duplicate function names }
Add(1, 2)      // calls the first overload
Add(1.0, 2.0) // calls the second overload

Parametric version using a type parameter:

func Add[T any](a, b T) T { return a + b }
fmt.Println(Add[int](1, 2))
fmt.Println(Add[string]("hello", "world")) // compile‑time error: + not defined for string

Defining a constraint interface to restrict types:

type Addable interface { type int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, uintptr, float32, float64, string }
func add[T Addable](a, b T) T { return a + b }

Using the standard comparable constraint for equality checks:

func findFunc[T comparable](a []T, v T) int { for i, e := range a { if e == v { return i } } return -1 }
fmt.Println(findFunc([]int{1,2,3,4,5,6}, 5)) // prints 4

Examples also cover generic slices, maps, queues, and utility functions such as max and min that operate on a custom minmax constraint.

The article concludes with a summary of the design goals of Go generics, their impact on code readability, compilation time, and runtime performance, and provides references to further reading.

ProgrammingGogenericstype-parametersConstraintsexamples
Tencent Cloud Developer
Written by

Tencent Cloud Developer

Official Tencent Cloud community account that brings together developers, shares practical tech insights, and fosters an influential tech exchange community.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.