Go 1.20 有哪些新特色? | 青训营笔记

147 阅读4分钟

这是我参与「第五届青训营 」伴学笔记创作活动的第 14 天

语言改变

  1. 预先声明的 comparable 约束现在也能被普通的可比较类型满足,例如接口,这将简化通用代码。
  1. unsafe包增加了三个函数:SliceData()String()StringData()。它们完成了独立于独立于实现的切片和字符串操作的函数集。
  • The functions SliceDataString, and StringData have been added to package unsafe. They complete the set of functions for implementation-independent slice and string manipulation.
  1. Go语言的类型转换规则已扩展为允许
  1. 语言规范了现在比较了数组元素和结构体字段比较的确切顺序,这阐明了在比较过程中出现panic会发生什么
  • The language specification now defines the exact order in which array elements and struct fields are compared. This clarifies what happens in case of panics during comparisons.

工具改进

  1. cover 工具现在可以收集整个程序的代码覆盖率情况,而不仅仅是单元测试
  • The cover tool now can collect coverage profiles of whole programs, not just of unit tests.
  1. go tool 不再依赖于$GOROOT/pkg 目录中预编译的标准库包存档,且不再随发行版一起提供,从而减少了下载量。标准库中的软件包根据需要构建,并像其他包一样缓存在构建缓存中。
  • go install不再往$GOROOT/pkg目录写文件。
  • go build不再检查$GOROOT/pkg下的文件。
  • Go发布包不再带有这些编译文件。
  • The go tool no longer relies on pre-compiled standard library package archives in the $GOROOT/pkg directory, and they are no longer shipped with the distribution, resulting in smaller downloads. Instead, packages in the standard library are built as needed and cached in the build cache, like other packages.
  1. go test -json 实现更加健壮。
  2. go buildgo install和其它编译相关的命令新增了一个-pgo标记参数,可以辅助开发者做程序优化。 新增了一个-cover标记参数,可以用来对编译出来的可执行程序做代码覆盖率收集。
  • The go buildgo install, and other build-related commands now accept a -pgo flag enabling profile-guided optimizations as well as a -cover flag for whole-program coverage analysis.
  1. 在没有C工具链的系统上,go命令默认禁用cgo。因此,当Go安装在没有C编译器的系统上时,它现在将对可选使用cgo的标准库中的包使用纯Go构建,而不是使用预分发的包存档(如上所述,这些存档已被删除)。
  • The go command now disables cgo by default on systems without a C toolchain. Consequently, when Go is installed on a system without a C compiler, it will now use pure Go builds for packages in the standard library that optionally use cgo, instead of using pre-distributed package archives (which have been removed, as noted above).
  1. go vet 工具报告了在并行运行的测试中可能发生的更多循环变量引用错误。
  • The vet tool reports more loop variable reference mistakes that may occur in tests running in parallel.

标准库添加

  1. 新增 crypto/ecdh 包明确支持通过 NIST 曲线和 Curve25519 进行的椭圆曲线 Diffie-Hellman 密钥交换。
  • The new crypto/ecdh package provides explicit support for Elliptic Curve Diffie-Hellman key exchanges over NIST curves and Curve25519.

2.新增函数 errors.Join 返回一个错误包装错误列表,如果错误类型实现了 Unwrap() []error 方法,则可以再次获得该错误列表。

  • The new function errors.Join returns an error wrapping a list of errors which may be obtained again if the error type implements the Unwrap() []error method.
  1. 新增 http.ResponseController 类型提供对 http.ResponseWriter 接口未处理的扩展的每个请求功能的访问。
  1. httputil.ReverseProxy 转发代理包含一个新的 Rewrite 钩子函数,取代了之前的 Director
  • The httputil.ReverseProxy forwarding proxy includes a new Rewrite hook function, superseding the previous Director hook.
  1. 新增 context.WithCancelCause 函数提供了一种方法来取消具有给定错误的上下文。可以通过调用新的 context.Cause 函数来检索该错误。
  1. 新增 os/exec.Cmd 字段 CancelWaitDelay 指定 Cmd 在其关联的 Context 被取消或其进程退出时的行为。
  • The new os/exec.Cmd fields Cancel and WaitDelay specify the behavior of the Cmd when its associated Contextis canceled or its process exits.

性能提高

  1. 编译器和垃圾收集器的改进减少了内存开销,并将整体 CPU 性能提高了 2%。
  • Compiler and garbage collector improvements have reduced memory overhead and improved overall CPU performance by up to 2%.
  1. 专门针对编译时间的工作导致构建改进高达 10%。这使构建速度与 Go 1.17 保持一致。
  • Work specifically targeting compilation times led to build improvements by up to 10%. This brings build speeds back in line with Go 1.17.