文章目录
- 前言
- 一、Go 1.18泛型版本安装、Go多版本安装
- 二、Go 1.18泛型版本新增语法详解
- 特点1:The syntax for function and type declarations now accepts type parameters.
- 特点2:Parameterized functions and types can be instantiated by following them with a list of type arguments in square brackets.
- 特点3:The `new token ~` has been added to the set of operators and punctuation.
- `Go近似符号~的使用:底层类型一样即可传参`
- Go ~更多用法
- 特点4:The syntax for Interface types now permits the embedding of arbitrary types (not just type names of interfaces) as well as union and ~T type elements. Such interfaces may only be used as type constraints. An interface now defines a set of types as well as a set of methods.
- 特点5:多加了2个关键字`any`和`comparable`
前言
没有泛型a generic function
的时候,如何实现计算最小值
一、Go 1.18泛型版本安装、Go多版本安装
两步:Go install
和Go download
# install
go install golang.org/dl/go1.18@latest
# download
go1.18 download
# show
go1.18 version
二、Go 1.18泛型版本新增语法详解
特点1:The syntax for function and type declarations now accepts type parameters.
特点2:Parameterized functions and types can be instantiated by following them with a list of type arguments in square brackets.
特点3:The new token ~
has been added to the set of operators and punctuation.
Go近似符号~的使用:底层类型一样即可传参
Go ~更多用法
特点4:The syntax for Interface types now permits the embedding of arbitrary types (not just type names of interfaces) as well as union and ~T type elements. Such interfaces may only be used as type constraints. An interface now defines a set of types as well as a set of methods.
特点5:多加了2个关键字any
和comparable
- The new predeclared identifier
any
is an alias for the empty interface. It may be used instead of interface{}. - The new predeclared identifier
comparable
is an interface that denotes the set of all types which can be compared using == or !=. It may only be used as (or embedded in) a type constraint.