Go 1.18 (2022-03-15)
Go 1.18 was released in March 2022 and is one of the most significant releases in the history of the Go language, introducing Generics, Fuzzing, and Workspaces.
Major Changes
Language
- Generics: Introduced support for parametric polymorphism, allowing functions and data structures to be written for multiple types.
- Type Parameters: Functions and type declarations now support type parameters.
- Constraints: Interfaces can now define sets of types to be used as type constraints.
any: Added the predefined identifieranyas an alias forinterface{}.comparable: Added the predefined identifiercomparable, representing the set of all comparable types.~Operator: Used to represent the underlying type set.
func Min[T constraints.Ordered](x, y T) T {
if x < y {
return x
}
return y
}
Toolchain
-
Fuzzing: The
go testcommand now has built-in support for fuzzing to help discover bugs in edge cases.go test -fuzz=FuzzMyFunc -
Workspaces: Introduced
go.workfiles and thego workcommand, supporting a multi-module workspace development mode. This facilitates simultaneous modification of multiple interdependent modules without modifyinggo.modreplace directives. -
go get:go getno longer builds or installs packages (it is now dedicated to managinggo.moddependencies). Usego installto install executables. -
GOAMD64: Introduced theGOAMD64environment variable, allowing the selection of the AMD64 architecture microarchitecture level (v1-v4) to leverage newer instruction sets for improved performance.
Performance
- Performance Improvements: Significant performance improvements (up to 20%) on Apple M1, ARM64, and PowerPC64 architectures.
- Compiler: Compilation speed may be slightly slower (about 15%) due to the introduction of generics, but the execution efficiency of the generated code is unaffected.
Standard Library
-
net/netip: Addednet/netippackage, defining a new IP address typeAddr, which is smaller, more efficient, immutable, and supports being used as a map key compared tonet.IP. -
strings/bytes:- Added
Cutfunction to simplify string splitting operations. - Added
Clonefunction to copy strings or byte slices.
before, after, found := strings.Cut("hello,world", ",") - Added
-
debug/buildinfo: Added package for reading build information embedded in Go binaries (module version, VCS info, build flags, etc.). -
sync:MutexandRWMutexaddedTryLockmethod. -
tls: TLS 1.0 and 1.1 are disabled by default for clients. -
crypto/x509: SHA-1 signed certificates are rejected by default.
References
For more details, please refer to the official release notes: Go 1.18 Release Notes