Interface for all numbers?

Issue

The generics tutorial uses this:

type Number interface {
    int64 | float64
}

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

Solution

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
}

Answered By – Endre Simo

Answer Checked By – Cary Denson (GoLangFix Admin)

Leave a Reply

Your email address will not be published.