官方透露大概在1.17版本会Release。
等待吧。
try catch都无所谓了,没有泛型太难受了。
新的泛型算法是这么写的,大家感兴趣的可以去试试。
package main
import (
"fmt"
)
// Ordered is a type constraint that matches any ordered type.
// An ordered type is one that supports the <, <=, >, and >= operators.
type Ordered interface {
type int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, uintptr, float32, float64, string
}
func Sum[T Ordered](s []T) T {
var sum T
for _, v := range s {
sum += v
}
return sum
}
func main() {
arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
res := Sum(arr)
fmt.Println(res)
}
尝试地址:
https://go2goplay.golang.org/
输出:
有疑问加站长微信联系(非本文作者)