Syntax
Type Constraints
Go does not support direct union types. You cannot do something like:Instead, you can use an interface to define a type set used as a generic constraint:The You can also use
| operator is only valid in generic constraints. It specifies that a type parameter can be one of several types.Then use it like:| directly without declaring a named interface:Examples
Example 1: Basic Generic Function
Without generics
Without generics