#1楼@zzustu 就在刚刚5分钟前,`golang.org/x/exp/slices` 包更新 [slices: consistently use E rather than Elem](https://github.com/golang/exp/commit/48e79f11773a3b8c224e7fb3f7149ef3f013f5fa) 😂
#1楼@zzustu `golang.org/x/exp/slices`包中已有切片泛型相关函数。
“代码中对泛型的声明太冗长了” ,对于这个问题最初其实很自然也是想用 E,R 等缩写。但是直到我看到 `golang.org/x/exp/slices` 包中的一些方法:https://github.com/golang/exp/blob/master/slices/sort.go 没使用缩写。
`golang.org/x/exp/slices` 片段代码
```go
// Sort sorts a slice of any ordered type in ascending order.
func Sort[Elem constraints.Ordered](x []Elem) {
n := len(x)
quickSortOrdered(x, 0, n, maxDepth(n))
}
// Sort sorts the slice x in ascending order as determined by the less function.
// This sort is not guaranteed to be stable.
func SortFunc[Elem any](x []Elem, less func(a, b Elem) bool) {
n := len(x)
quickSortLessFunc(x, 0, n, maxDepth(n), less)
}
```
最近的评论