Parametric Constraints
Interfaces and structs can also take type parameters, making them “parametric.”
Generic Interfaces
You can define an interface that works with a variable type.
type store[P product] interface {
Sell(P)
}
type product interface {
Price() float64
}
Generic Structs
Structs can also be generic, allowing them to store any type while maintaining type safety.
type Box[T any] struct {
Content T
}
intBox := Box[int]{Content: 42}
strBox := Box[string]{Content: "Hello"}
Usage
Generic interfaces are useful when you want to define a behavior that depends on a specific type, but you want to keep that type flexible until the interface is actually implemented.