Go泛型系列:Slices 包讲解

开发 后端
目前,Go 泛型的主要设计者 ianlancetaylor 完成了 slices 和 maps 包的开发,代码提交到了 golang.org/x/exp 中,如果经过使用、讨论等,社区认可后,预计在 1.19 中会合入标准库中。

[[437049]]

大家好,我是 polarisxu。

前段时间,Russ Cox 明确了泛型相关的事情,原计划在标准库中加入泛型相关的包,改放到 golang.org/x/exp 下。

目前,Go 泛型的主要设计者 ianlancetaylor 完成了 slices 和 maps 包的开发,代码提交到了 golang.org/x/exp 中,如果经过使用、讨论等,社区认可后,预计在 1.19 中会合入标准库中。

今天,通过学习 slices 包,掌握 Go 泛型的使用方法。

01 为什么增加 slices 包

标准库有 bytes 和 strings 包,分别用来处理 []byte 和 string 类型,提供了众多方便的函数,但对普通的 slice,却没有相关的包可以使用。

比如 bytes 和 strings 都有 Index 函数,用来在 []byte 或 string 查找某个 byte 或字符串的索引。对于普通的 slice,没法写一大堆包来处理,只能用户自己实现,这也是没有泛型的弊端。

提供 bytes 和 strings,主要是因为它们使用频率高

现在有了泛型,可以实现一些便利的 slice 操作方法,必须要针对某一个具体类型的 slice 都实现一遍相同的功能。

02 constraints 包

继续讲解 slices 包之前,先看看 contraints 包。

该包定义了一组用于类型参数(泛型)的有用约束,这个包已经确定在 Go 1.18 标准库中包含,截止目前(2021.11.27),该包定义了 6 个约束类型:

  1. // Signed is a constraint that permits any signed integer type. 
  2. // If future releases of Go add new predeclared signed integer types, 
  3. // this constraint will be modified to include them. 
  4. type Signed interface { 
  5.  ~int | ~int8 | ~int16 | ~int32 | ~int64 
  6.  
  7. // Unsigned is a constraint that permits any unsigned integer type. 
  8. // If future releases of Go add new predeclared unsigned integer types, 
  9. // this constraint will be modified to include them. 
  10. type Unsigned interface { 
  11.  ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr 
  12.  
  13. // Integer is a constraint that permits any integer type. 
  14. // If future releases of Go add new predeclared integer types, 
  15. // this constraint will be modified to include them. 
  16. type Integer interface { 
  17.  Signed | Unsigned 
  18.  
  19. // Float is a constraint that permits any floating-point type. 
  20. // If future releases of Go add new predeclared floating-point types, 
  21. // this constraint will be modified to include them. 
  22. type Float interface { 
  23.  ~float32 | ~float64 
  24.  
  25. // Complex is a constraint that permits any complex numeric type. 
  26. // If future releases of Go add new predeclared complex numeric types, 
  27. // this constraint will be modified to include them. 
  28. type Complex interface { 
  29.  ~complex64 | ~complex128 
  30.  
  31. // Ordered is a constraint that permits any ordered type: any type 
  32. // that supports the operators < <= >= >. 
  33. // If future releases of Go add new ordered types, 
  34. // this constraint will be modified to include them. 
  35. type Ordered interface { 
  36.  Integer | Float | ~string 

前面 3 个是整型相关类型约束,Float 是浮点型约束,Complex 是负数类型约束,而 Ordered 表示支持排序的类型约束,表示支持大小比较的类型。

之前文章:《Go泛型系列:Go1.18 类型约束那些事》提到,约束语法变更了,一个是 | 符号,一个是 ~,上面定义中,很多地方都用到了 ~ 符号,它表示出了类型自身,底层类型是它的类型也适用该约束。

03 slices 包详解

目前,slices 包有 14 个函数,可以分成几组:

  • slice 比较
  • 元素查找
  • 修改 slice
  • 克隆 slice

其中,修改 slice 分为插入元素、删除元素、连续元素去重、slice 扩容和缩容。

slice 比较

比较两个 slice 中的元素,细分为是否相等和普通比较:

  1. func Equal[E comparable](s1, s2 []E) bool 
  2.  
  3. func EqualFunc[E1, E2 any](s1 []E1, s2 []E2, eq func(E1, E2) bool) bool 
  4.  
  5. func Compare[E constraints.Ordered](s1, s2 []E) int 
  6.  
  7. func CompareFunc[E1, E2 any](s1 []E1, s2 []E2, cmp func(E1, E2) intint 

其中 comparable 约束是语言实现的(因为很常用),表示可比较约束(相等与否的比较)。主要,其中的 E、E1、E2 等,只是泛型类型表示,你定义时,可以用你喜欢的,比如 T、T1、T2 等。

看一个具体的实现:

  1. func Equal[E comparable](s1, s2 []E) bool { 
  2.  
  3. if len(s1) != len(s2) { 
  4.  
  5. return false 
  6.  
  7.  
  8. for i, v1 := range s1 { 
  9.  
  10. v2 := s2[i] 
  11.  
  12. if v1 != v2 { 
  13.  
  14. return false 
  15.  
  16.  
  17.  
  18. return true 
  19.  

没有什么特别的,只不过把 s1、s2 当成同类型的 slice 进行操作而已。

元素查找

在 slice 中查找某个元素,分为普通的所有查找和包含判断:

  1. func Index[E comparable](s []E, v E) int 
  2.  
  3. func IndexFunc[E any](s []E, f func(E) bool) int 
  4.  
  5. func Contains[E comparable](s []E, v E) bool 

其中,IndexFunc 的类型参数没有使用任何约束(即用的 any),说明查找是通过 f 参数进行的,它的实现如下:

  1. func IndexFunc[E any](s []E, f func(E) bool) int { 
  2.  
  3. for i, v := range s { 
  4.  
  5. if f(v) { 
  6.  
  7. return i 
  8.  
  9.  
  10.  
  11. return -1 
  12.  

参数 f 是一个函数,它接收一个参数,类型是 E,是一个泛型,和 IndexFunc 的第一个参数类型 []E 的元素类型保持一致即可,因此可以直接将遍历 s 的元素传递给 f。

修改 slice

一般不建议做相关操作,因为性能较差。如果有较多这样的需求,可能需要考虑更换数据结构。

  1. // 往 slice 的位置 i 处插入元素(可以多个) 
  2. func Insert[S ~[]E, E any](s S, i int, v ...E) S 
  3. // 删除 slice 中 i 到 j 的元素,即删除 s[i:j] 元素 
  4. func Delete[S ~[]E, E any](s S, i, j int) S 
  5. // 将连续相等的元素替换为一个,类似于 Unix 的 uniq 命令。Compact 修改切片的内容,它不会创建新切片 
  6. func Compact[S ~[]E, E comparable](s S)  
  7. func CompactFunc[S ~[]E, E any](s S, eq func(E, E) bool) S 
  8. // 增加 slice 的容量,至少增加 n 个 
  9. func Grow[S ~[]E, E any](s S, n int) S 
  10. // 移除没有使用的容量,相当于缩容 
  11. func Clip[S ~[]E, E any](s S) S 

以上类型约束都包含了两个:

  • S ~[]E:表明这是一个泛型版 slice,这是对 slice 的约束。注意 [] 前面的 ~,表明支持自定义 slice 类型,如 type myslice []int
  • E any 或 E comparable:对上面 slice 元素类型的约束。

克隆 slice

即获得 slice 的副本,会进行元素拷贝,注意,slice 中元素的拷贝是浅拷贝,非值类型不会深拷贝。

  1. func Clone[S ~[]E, E any](s S) S { 
  2.  
  3. // Preserve nil in case it matters. 
  4.  
  5. if s == nil { 
  6.  
  7. return nil 
  8.  
  9.  
  10. return append(S([]E{}), s...) 
  11.  

04 总结

因为泛型的存在,同样的功能,对不同类型的 slice 再也不用写多份代码。因为一些功能很常见,因此 Go 官方将其封装,将来会在标准库中提供。

出于谨慎考虑,slices 包不会在 1.18 中包含,如果你需要用到 slices 中的功能,可以采用从 slices 代码中复制的方式,个人觉得依赖 golang.org/x/exp 还是不太好。

slices 源码地址:https://github.com/golang/exp/blob/master/slices/slices.go。

责任编辑:武晓燕 来源: polarisxu
相关推荐

2023-11-03 14:02:04

Go切片泛型库

2021-08-09 10:36:20

GoSlices Maps

2021-12-05 23:45:23

Go泛型Maps

2021-09-29 18:17:30

Go泛型语言

2021-12-01 08:29:17

Go泛型Maps

2010-01-06 10:43:49

.NET Framew

2022-04-28 12:00:34

Go泛型版排序

2022-03-28 13:34:26

Go泛型部署泛型

2023-11-29 08:19:45

Go泛型缺陷

2023-05-05 08:51:18

Go语言泛型

2021-12-15 10:23:56

Go 1.18 Bet语言泛型

2022-04-15 09:55:59

Go 泛型Go 程序函数

2022-03-29 11:48:40

Go泛型测试

2021-01-14 05:20:48

Go语言泛型

2021-11-01 12:41:39

Go

2021-12-30 18:34:29

缓存GoSinglefligh

2021-12-13 08:52:42

Go 泛型

2021-12-28 07:20:44

泛型Go场景

2023-09-27 08:26:48

Go标准库函数

2021-10-29 10:55:07

Go 泛型语言
点赞
收藏

51CTO技术栈公众号