go: 官方提供的泛形约束(constraints) 在使用泛型的时候,难免会需要定义一些约束,比如这样: ``` type Number interface { ~int64 | ~int32 | ~int8 | ~uint64 | ~uint32 | ~uint16 | ~uint8 | ~int } ``` 其实,在官方已经定义好了,直接用就好。 地址在: golang.org/x/exp/constraints 提供了 ``` type Complex type Float type Integer type Ordered type Signed type Unsigned ``` 这几种约束,好好享用吧。 来自 大脸猪 写于 2023-04-07 10:29 -- 更新于2023-04-07 10:32 -- 0 条评论