Go 1.20 (2023-02-01)
Go 1.20 was released in February 2023, focusing on performance optimization, compiler improvements, and standard library enhancements.
Major Changes
Language
-
Slice to Array Conversion: Support for converting slices directly to arrays (Go 1.17 already supported conversion to array pointers).
s := []int{1, 2, 3}
a := [3]int(s) // copy -
unsafePackage: AddedSliceData,String, andStringDatafunctions, providing implementation-independent capabilities for constructing and deconstructing slices/strings. -
Comparable Constraint: The
comparableconstraint can now be satisfied by ordinary interfaces, even if the type arguments are not strictly comparable (comparison may panic at runtime).
Toolchain
-
PGO (Preview): Introduced preview support for Profile-guided Optimization (PGO). Using the
-pgoflag, the compiler can use runtime profile information for specific optimizations (such as more aggressive inlining), improving performance by about 3-4%. -
go build -cover: Support for collecting code coverage for binaries, not just tests.go build -cover -o myapp
GOCOVERDIR=covdata ./myapp -
No Precompiled Standard Library:
$GOROOT/pkgno longer stores precompiled archive files for the standard library. Standard library packages are now built and cached on demand like other packages, reducing the size of the Go distribution. -
vetTool: Improved detection of loop variable capture (for nested functions) and added detection for time format errors.
Performance
- Compiler: Optimized inlining and bounds check elimination, improving code execution efficiency. Build speed improved by about 10% compared to Go 1.19.
- Memory Management: Optimized internal data structures for garbage collection, reducing memory overhead and improving CPU performance.
Standard Library
errors: Support for wrapping multiple errors.fmt.Errorfsupports multiple%w.- Added
errors.Joinfunction for combining multiple errors.
context: AddedWithCancelCauseandCause, allowing specific cancellation causes to be passed and retrieved when cancelling a context.crypto/ecdh: Addedcrypto/ecdhpackage, providing explicit support for NIST curves and Curve25519.net/http:- Added
ResponseControllertype for accessing extended features ofResponseWriter(such asSetReadDeadline,SetWriteDeadline). httputil.ReverseProxyaddedRewritehook, replacingDirector, providing more powerful request rewriting capabilities.
- Added
time: Added constantsDateTime,DateOnly,TimeOnly. AddedTime.Comparemethod.
Platform Support
- Windows: Go 1.20 is the last version to support Windows 7, 8, Server 2008, and Server 2012.
- macOS: Go 1.20 is the last version to support macOS 10.13 High Sierra and 10.14 Mojave.
- FreeBSD/RISC-V: Added experimental support for
GOOS=freebsd,GOARCH=riscv64.
References
For more details, please refer to the official release notes: Go 1.20 Release Notes