Go 1.16 (2021-02-16)
Go 1.16 was released in February 2021, introducing file embedding (embed) support and enabling Go Modules by default.
Major Changes
Toolchain
-
Go Modules Enabled by Default: The default value of the
GO111MODULEenvironment variable is nowon. The Go command now runs in Module mode by default, regardless of whether the current directory contains ago.modfile.- The
go installcommand now supports thepkg@versionsyntax for installing executables of a specific version without affecting the dependencies of the current project. For example:go install golang.org/x/tools/gopls@latest. go buildandgo testno longer automatically modifygo.modandgo.sum. If dependencies need to be updated,go mod tidyorgo getmust be run explicitly.
- The
-
Embedding Static Files (Embed): Introduced the
//go:embeddirective, allowing static files (such as HTML, CSS, images, etc.) to be directly embedded into the compiled Go binary.package main
import (
"embed"
"fmt"
)
//go:embed hello.txt
var f embed.FS
func main() {
data, _ := f.ReadFile("hello.txt")
fmt.Print(string(data))
} -
Linker Optimization: Linker speed improved by 20-25%, and memory usage reduced by 5-15%.
-
go build -overlay: Supports the-overlayflag, mainly for editor tools (such as gopls). -
go vet: Added checks for incorrect use oftesting.Tingoroutines.
Runtime
- Apple Silicon (M1) Support: Added
darwin/arm64port, officially supporting running Go programs natively on Macs with Apple Silicon (M1) chips. runtime/metrics: Added a new package providing a more stable and general interface for reading runtime metrics.GODEBUG=inittrace=1: Setting this environment variable traces the execution time and memory allocation ofinitfunctions, helping to analyze startup performance.- Linux Memory Release: On Linux, the runtime now defaults to using
MADV_DONTNEEDto quickly release memory to the operating system, instead ofMADV_FREE.
Standard Library
io/fs: Introduced thefs.FSinterface, defining an abstraction for read-only file systems. Many packages in the standard library (such asnet/http,html/template) have been adapted to this interface.embed: Added theembedstandard library package, providing methods to access embedded files.io/ioutilDeprecation: Theio/ioutilpackage is marked as deprecated (although still usable, migration is recommended). Its functionality has been moved to theioandospackages:ioutil.Discard->io.Discardioutil.NopCloser->io.NopCloserioutil.ReadAll->io.ReadAllioutil.ReadDir->os.ReadDir(returnsos.DirEntryslice)ioutil.ReadFile->os.ReadFileioutil.WriteFile->os.WriteFileioutil.TempDir->os.MkdirTempioutil.TempFile->os.CreateTemp
os/signal: AddedNotifyContextfunction, creating a Context that is canceled when a signal is received.path/filepath: AddedWalkDirfunction, which is more efficient thanWalk(avoids callingos.Lstatfor every file).net/http: Adjusted default behavior forSameSitecookie attribute.strconv:ParseFloatuses the faster Eisel-Lemire algorithm.
References
For more details, please refer to the official release notes: Go 1.16 Release Notes