Define type parameters in square brackets after the struct name to create reusable, type-safe data structures.
You use generics with structs by defining type parameters in square brackets after the struct name and using them as field types.
type Container[T any] struct {
Value T
}
// Usage
c := Container[int]{Value: 42}
This allows the struct to hold values of any type T while maintaining type safety.
Generics let you write a single struct definition that works with any data type, like a box that can hold a toy, a book, or a tool without needing a separate box for each item. This saves you from copying and pasting code for every type you want to store. You use them whenever you need a flexible container or data structure that handles different kinds of data safely.