可以在: https://go2goplay.golang.org/ 去尝试下go的泛型
官方表示有可能在go1.17版本中添加,最早时间可能是2021年8月份
大概代码如下:
package main
import (
"fmt"
)
func Print(type T)(s []T) {
for _, v := range s {
fmt.Print(v)
}
}
func main() {
Print([]string{"Hello, ", "playground\n"})
Print(int)([]int{1, 2, 3})
}
更多查看:
https://github.com/golang/pro…
https://go.googlesource.com/p…
发表回复