安装 Go 1.18beta1
$ go install golang.org/dl/go1.18beta1@latest
go: downloading golang.org/dl v0.0.0-20211214194107-65ed43b8dfff
2 . 运行以下命令进行更新
$ go1.18beta1 download
Downloaded 0.0% ( 16384 / 143162528 bytes) ...
Downloaded 5.2% ( 7454704 / 143162528 bytes) ...
Downloaded 11.9% ( 16973760 / 143162528 bytes) ...
Downloaded 18.6% ( 26640192 / 143162528 bytes) ...
Downloaded 25.5% ( 36486896 / 143162528 bytes) ...
Downloaded 32.1% ( 46006032 / 143162528 bytes) ...
Downloaded 39.0% ( 55852640 / 143162528 bytes) ...
Downloaded 46.1% ( 66059792 / 143162528 bytes) ...
Downloaded 52.5% ( 75169264 / 143162528 bytes) ...
Downloaded 59.0% ( 84458912 / 143162528 bytes) ...
Downloaded 65.1% ( 93224256 / 143162528 bytes) ...
Downloaded 70.4% (100744512 / 143162528 bytes) ...
Downloaded 77.2% (110591200 / 143162528 bytes) ...
Downloaded 83.9% (120159376 / 143162528 bytes) ...
Downloaded 90.7% (129842224 / 143162528 bytes) ...
Downloaded 95.2% (136264736 / 143162528 bytes) ...
Downloaded 100.0% (143162528 / 143162528 bytes)
Unpacking /Users/xxx/sdk/go1.18beta1/go1.18beta1.darwin-amd64.tar.gz ...
Success. You may now run 'go1.18beta1'
3 . 测试 go1.18beta1 是否安装成功
$ go1.18beta1 version
go version go1.18beta1 darwin/amd64
4 . 为了我们更方便的使用 Go 命令,我们可以给 go1.18beta1 重命名。
$ alias go=go1.18beta1
$ go version
go version go1.18beta1 darwin/amd64
$ mkdir generics
$ cd generics
$ go mod init github.com/yangwenmai/generics
go: creating new go.mod: module github.com/yangwenmai/generics
2 . 创建文件,编写 Go 代码
$ vim main.go
本示例来自于 Go 官网:https://go.dev/doc/tutorial/generics
package main
import "fmt"
type Number interface {
int64 | float64
}
func main() {
// Initialize a map for the integer values
ints := map[string]int64{
"first": 34,
"second": 12,
}
// Initialize a map for the float values
floats := map[string]float64{
"first": 35.98,
"second": 26.99,
}
fmt.Printf("Non-Generic Sums: %v and %v\n",
SumInts(ints),
SumFloats(floats))
fmt.Printf("Generic Sums: %v and %v\n",
SumIntsOrFloats[string, int64](ints),
SumIntsOrFloats[string, float64](floats))
fmt.Printf("Generic Sums, type parameters inferred: %v and %v\n",
SumIntsOrFloats(ints),
SumIntsOrFloats(floats))
fmt.Printf("Generic Sums with Constraint: %v and %v\n",
SumNumbers(ints),
SumNumbers(floats))
}
// SumInts adds together the values of m.
func SumInts(m map[string]int64) int64 {
var s int64
for _, v := range m {
s += v
}
return s
}
// SumFloats adds together the values of m.
func SumFloats(m map[string]float64) float64 {
var s float64
for _, v := range m {
s += v
}
return s
}
// SumIntsOrFloats sums the values of map m. It supports both floats and integers
// as map values.
func SumIntsOrFloats[K comparable, V int64 | float64](m map[K]V) V {
var s V
for _, v := range m {
s += v
}
return s
}
// SumNumbers sums the values of map m. Its supports both integers
// and floats as map values.
func SumNumbers[K comparable, V Number](m map[K]V) V {
var s V
for _, v := range m {
s += v
}
return s
}
你还可以直接在 Go Playground 上直接测试以上代码。
https://go.dev/play/?v=gotip
如图:
我核对了当前安装的 go1.18beta1 的版本,是修改 interface{} 为 any,但是当我尝试把 type Number interface
改为 type Number any
会直接报错:./main.go:21:17: syntax error: unexpected { after top level declaration
,有谁知道这是为什么?
欢迎各位留言评论~
Copyright© 2013-2020
All Rights Reserved 京ICP备2023019179号-8