Go Generics Best Practices and Common Patterns

Use Go generics with type parameters and constraints to create reusable, type-safe functions and data structures without code duplication.

Use type parameters with constraints to write reusable functions and types while adhering to Go's design principles of simplicity and explicitness. Define constraints using interface{} with type sets or methods to restrict valid types, then apply them to functions or structs using [T constraint] syntax.

package main

import "fmt"

type Number interface {
    int | int32 | int64 | float32 | float64
}

func Max[T Number](a, b T) T {
    if a < b {
        return b
    }
    return a
}

type Stack[T any] struct {
    items []T
}

func (s *Stack[T]) Push(item T) {
    s.items = append(s.items, item)
}

func main() {
    fmt.Println(Max(1, 2))
    fmt.Println(Max(1.5, 2.5))
    var s Stack[string]
    s.Push("hello")
}