Go lacks union types, so use interfaces or type switches to handle multiple types like int and float64.
Go does not support union type constraints like ~int | ~float64 because it lacks a union type system; you must use interfaces or type switches instead. Use an interface to accept multiple types or a type switch to handle them explicitly at runtime.
func Process(v any) {
switch v := v.(type) {
case int, float64:
// Handle numeric types
default:
// Handle other types
}
}
Go does not have a feature that lets you combine different types into a single 'union' type like some other languages do. Instead, you use a generic placeholder called 'any' to accept anything, or you write a specific check to see which type you received before using it. Think of it like a mail slot that accepts any letter, but you have to open it to see if it's a bill or a postcard before you know what to do.