Golang:Go1.18泛型使用详解


前言

没有泛型a generic function的时候,如何实现计算最小值

通用

一、Go 1.18泛型版本安装、Go多版本安装

两步:Go installGo 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个关键字anycomparable

  • 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.
  • 在这里插入图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值