golang-泛型预览版发布

4次阅读

共计 305 个字符,预计需要花费 1 分钟才能阅读完成。

可以在: 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…

正文完
 0