Fundamentals 23 min read

Understanding Go Generics: Concepts, History, and Practical Examples

This article introduces Go's newly added generic support, explains its concepts, historical development, usage examples, constraints, and best practices, while providing code snippets for functions, slices, maps, queues, and pointers, helping developers grasp how to write type‑safe reusable code in Go.

High Availability Architecture
High Availability Architecture
High Availability Architecture
Understanding Go Generics: Concepts, History, and Practical Examples

Go 1.17 introduced an experimental version of generics, paving the way for the full generic implementation in Go 1.18. Generics allow developers to write type‑parameterized functions and types, reducing code duplication and improving type safety.

The core syntax uses square brackets to declare type parameters, optionally constrained by interfaces. For example, a generic addition function can be written as: func Add[T any](a, b T) T { return a + b } Constraints can be expressed with interfaces that list permissible operations or type sets. The standard library now provides a constraints package with common constraints such as Ordered, Signed, and Unsigned.

type Ordered interface { type int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, uintptr, float32, float64, string }

Practical examples include:

Generic slice printing:

func printSlice[T any](s []T) { for _, v := range s { fmt.Printf("%v ", v) } fmt.Println() }

Generic map transformation:

func mapFunc[T any, M any](a []T, f func(T) M) []M { n := make([]M, len(a)); for i, e := range a { n[i] = f(e) }; return n }

Generic queue implementation:

type queue[T any] []T
func (q *queue[T]) enqueue(v T) { *q = append(*q, v) }
func (q *queue[T]) dequeue() (T, bool) { if len(*q) == 0 { var zero T; return zero, false }; r := (*q)[0]; *q = (*q)[1:]; return r, true }

Generic pointer helper: func pointerOf[T any](v T) *T { return &v } The article also discusses the evolution of generics in Go, the "generic dilemma" concerning trade‑offs between compile time, runtime performance, and programmer convenience, and provides guidance on when to use generics versus concrete types.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

programmingGoGenericsType ParametersConstraintsexamples
High Availability Architecture
Written by

High Availability Architecture

Official account for High Availability Architecture.

0 followers
Reader feedback

How this landed with the community

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.