Union interface
A union interface is a Go generic constraint whose type set permits multiple alternative types or underlying types.
Learn Go with Pocket-Sized Projects
A union interface is a Go generic constraint whose type set combines multiple permitted types or underlying types.
- How it is used
- Restricts generic functions to selected integer types while permitting shared operations such as addition.
- How deep it goes
- Defines exact and underlying-type unions, then applies them to generic constraints without developing a broader constraint design.
- Examples
- Uses int, int32, int64, a named age type, and summable constraints with and without tilde syntax.
- What you can do after
- Apply it via Go generics to accept a deliberate set of numeric types and use their common operators.
Read how Learn Go with Pocket-Sized Projects explains union interface →
100 Go Mistakes and How to Avoid Them
The union operator | combines type terms in a Go generic constraint to permit multiple alternative types.
- How it is used
- Restricts generic type arguments to a defined set of alternative types.
- How deep it goes
- Defines union syntax through getKeys and contrasts exact types with underlying-type approximations using ~.
- Examples
- Uses customConstraint, ~int | ~string, and the generic getKeys map helper.
- What you can do after
- Apply it via Go generic constraints to admit selected alternative key types in reusable functions.
Read how 100 Go Mistakes and How to Avoid Them explains union interface →
Related concepts
- type constraintkind of · A type constraint is a rule that restricts admissible types or type arguments by requiring specified relationships, structures, properties, or supported operations.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.

