[学习笔记] Golang:Golang 1.16 新特性简明介绍
Source: Dev.to
TL;DR
本文将介绍:
How to install the Golang 1.16 preview version
如果你想尝试,但 Homebrew 仍未提供 Golang 版本(截至 2021‑02‑19),可以在本地安装测试版:
go get golang.org/dl/go1.16
go1.16 download
编译好的二进制文件会被安装到本地。之后可以使用 go1.16 build 或 go1.16 test 等命令。
Main list of new features in 1.16
Apple Silicon(即当前的 Apple M1 芯片)支持
Go 1.16 正式支持 Apple Silicon(64‑bit ARM)。使用以下编译器参数:
GOOS=darwin GOARCH=arm64 # for macOS on Apple M1
(iOS 则使用 GOOS=darwin GOARCH=ios/arm64。)
为 Apple M1 编译二进制:
env GOOS=darwin GOARCH=arm64 go build
Go Module Retract
版本撤回允许模块作者标记特定版本为有问题。详见该主题的完整文章:
Go 1.16 New Feature “Version Retraction (Removal)” (Go Modules retraction)
Embedding Files(在项目中包含静态文件)
在 Go 1.16 之前,静态文件需要借助 go-bindata、packr 或 pkger 等变通方案。Go 1.16 引入了原生的 embed 包来支持文件嵌入。
假设项目结构如下:
.
├── go.mod
├── main.go
├── static
│ └── css
│ └── main.css
├── templates
│ └── index.html.tmpl
└── title.txt
你可以直接在 Go 代码中嵌入文件。完整示例请参见 gist。
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)