[Learning Notes] Golang: A Simple Introduction to New Features in Golang 1.16
Source: Dev.to
TL;DR
This article will introduce:
How to install the Golang 1.16 preview version
If you want to try it out and there is no Golang version supported on Homebrew yet (as of 2021‑02‑19), you can install a test version locally:
go get golang.org/dl/go1.16
go1.16 download
The compiled binary will be installed locally. You can then run commands such as go1.16 build or go1.16 test.
Main list of new features in 1.16
Apple Silicon (i.e., the current Apple M1 Chip) support
Go 1.16 officially supports Apple Silicon (64‑bit ARM). Use the following compiler parameters:
GOOS=darwin GOARCH=arm64 # for macOS on Apple M1
(For iOS you would use GOOS=darwin GOARCH=ios/arm64.)
Compile a binary for Apple M1 with:
env GOOS=darwin GOARCH=arm64 go build
Go Module Retract
Version retraction allows module authors to mark specific versions as problematic. See the detailed article on the topic:
Go 1.16 New Feature “Version Retraction (Removal)” (Go Modules retraction)
Embedding Files (including static files in the project)
Prior to Go 1.16, static files had to be handled with workarounds such as go-bindata, packr, or pkger. Go 1.16 introduces native support via the embed package.
Assuming the following project layout:
.
├── go.mod
├── main.go
├── static
│ └── css
│ └── main.css
├── templates
│ └── index.html.tmpl
└── title.txt
You can embed files directly in your Go code. See the example gist for a complete demonstration.
Related information
- Go Doc: Embed Files
- Embedding static files in a Go binary using go embed (Medium)
- How to embed files into Go binaries (Stack Overflow)