這個問題在這里已經有了答案: 如何撰寫一個接受任何數值型別的泛型函式? (2 個回答) 12 天前關閉。
泛型教程使用這個:
type Number interface {
int64 | float64
}
golang中所有整數和浮點型別都沒有介面嗎?
uj5u.com熱心網友回復:
您可以宣告一個新的型別約束,它集成了constraints.Float和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
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/494546.html
下一篇:Java-獲取每個子類的實體
