Go Tutorial #22: Generics — Type Parameters in Go
In the previous tutorial, you learned API best practices. Now let’s explore one of Go’s most powerful features — generics. Before Go 1.18, you had two choices for writing reusable code: use interface{} (losing type safety) or write the same function for every type. Generics solve this problem. You write a function once, and it works with any type that meets your constraints. The Problem Without Generics Imagine you need a function to find the minimum value in a slice. Without generics, you write one function per type: ...