2

The generics tutorial uses this:

type Number interface {
    int64 | float64
}

Is there no interface for all integer and float types in golang?

blackgreen
  • 34,072
  • 23
  • 111
  • 129
guettli
  • 25,042
  • 81
  • 346
  • 663
  • You can declare your own set using `Signed` and `Float` from here: https://pkg.go.dev/golang.org/x/exp/constraints or don't use `constraints` package and define the set from the builtins entirely. – mkopriva Jun 10 '22 at 06:52
  • It should be `Integer` and `Float`. – icza Jun 10 '22 at 06:55
  • 1
    As illustrated by the above comments, it's not clear whether "all numbers" would include unsigned types or not. That's probably why it doesn't exist. – Hymns For Disco Jun 10 '22 at 06:58

1 Answers1

1

You can declare a new type constraint which integrates constraints.Float and constraints.Integer.

// Number is a custom type set of constraints extending the Float and Integer type set from the experimental constraints package.
type Number interface {
    constraints.Float | constraints.Integer
}
Endre Simo
  • 11,330
  • 2
  • 40
  • 49