Last Updated: May 22, 2026
A type parameter on its own ([T any]) lets you store and pass values of type T, but you can't actually do much with them. You can't call t.Price(), you can't use t as a map key, and you can't compare two T values with ==. A type constraint is the contract that fixes this: it tells the compiler what the generic code is allowed to assume about T, and in return the compiler refuses to compile any call where the supplied type doesn't honour that contract. This lesson covers the two predeclared constraints (any and comparable), how to use any interface as a constraint when methods are all you need, and how to compose constraints with embedding.