今天凌晨,Go 1.18正式发布。
新版本带来了许多变化:
泛型
-
The syntax for function and type declarations now accepts type parameters.
-
Parameterized functions and types can be instantiated by following them with a list of type arguments in square brackets.
-
The new token
~has been added to the set of operators and punctuation. -
The syntax for Interface types now permits the embedding of arbitrary types (not just type names of interfaces) as well as union and
~Ttype 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. -
The new predeclared identifier
anyis an alias for the empty interface. It may be used instead ofinterface{}. -
The new predeclared identifier
comparableis 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.
不过,目前泛型的使用还是有许多的限制。并不能像C++或Java那样完善。需要特别注意。
移植性
AMD64
Go 1.18 引入了新的环境变量,该变量在编译时选择 AMD64 架构的最低目标版本。Allowed values are v1, v2, v3, or v4. GOAMD64默认值是 v1。
Linux
Go 1.18 需要 Linux 内核版本 2.6.32 或更高版本。
工具
模糊
Go 1.18 包括模糊测试提案中描述的模糊测试的实现。
编译器
Go 1.17 开始, amd64 架构支持基于寄存器的参数传递。
Go 1.18 扩展了支持的平台,包括所有操作系统上的 64 位 ARM (GOARCH=arm64)、大端和小端 64 位 PowerPC (GOARCH=ppc64, ppc64le ),以及 64 位 x86 体系结构 (GOARCH=amd64)。在 64 位 ARM 和 64 位 PowerPC 系统上,基准测试显示典型的性能提升为 10% 或更多。
由于编译器中与支持泛型相关的更改,Go 1.18 编译速度可能比 Go 1.17 编译速度慢大约 15%。编译后的代码的执行时间不受影响。打算提高Go 1.19中编译器的速度。
连接器
链接器发出的重定位要少得多。因此,大多数代码库将链接得更快,链接所需的内存更少,并生成更小的二进制文件。
核心库
新软件包debug/buildinfo
新软件包net/netip
默认情况下,客户端禁用 TLS 1.0 和 1.1
拒绝 SHA-1 证书
对库的细微更改
其他
因为新增了泛型,函数的命名规则必定发生改变。
调用约定的改变,必然影响堆栈信息扫描。
gopclntab 预计也会有比较大的改动。
这些都是有趣的事,值得花时间研究研究。
文章评论