Golang:Go1.18泛型使用详解

文章目录

  • 前言
    • 没有泛型`a generic function`的时候,如何实现计算最小值
  • 一、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的时候,如何实现计算最小值

Golang:Go1.18泛型使用详解_第1张图片

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

两步:Go installGo download

# install
go install golang.org/dl/go1.18@latest
# download
go1.18 download
# show
go1.18 version

Golang:Go1.18泛型使用详解_第2张图片

二、Go 1.18泛型版本新增语法详解

Golang:Go1.18泛型使用详解_第3张图片

特点1:The syntax for function and type declarations now accepts type parameters.

Golang:Go1.18泛型使用详解_第4张图片

特点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.

Golang:Go1.18泛型使用详解_第5张图片

Go近似符号~的使用:底层类型一样即可传参

Golang:Go1.18泛型使用详解_第6张图片

Go ~更多用法

Golang:Go1.18泛型使用详解_第7张图片
Golang:Go1.18泛型使用详解_第8张图片

特点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.

Golang:Go1.18泛型使用详解_第9张图片

特点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.
  • Golang:Go1.18泛型使用详解_第10张图片

你可能感兴趣的:(#,L1:编程语言,golang,开发语言,后端)