# Go 1.16

Go 1.16 發布於 2021-02-16，目前最新修補版為 `go1.16.15`，**已停止支援**，請升級到 `go1.27.4`。

## 版本介紹

Go 1.16 有兩項改變了寫 Go 的日常：一是 `//go:embed` 指示，能把靜態檔案直接編進執行檔，部署時只要一個檔案；二是新的 `io/fs` 套件定義了統一的唯讀檔案系統介面 `fs.FS`。此外模組模式成為預設（`GO111MODULE=on`），並正式支援 Apple Silicon（`darwin/arm64`）。

### 工具

- 模組模式預設開啟，不論目錄中是否有 `go.mod`；要回到舊行為需設定 `GO111MODULE=auto`。
- `go build`、`go test` 預設不再自動修改 `go.mod` 與 `go.sum`，需要調整時用 `go mod tidy` 或 `go get`。
- `go install example.com/cmd@v1.0.0` 可以帶版本安裝工具，且不影響目前模組的依賴；這成為安裝執行檔的建議方式。
- `go.mod` 新增 `retract` 指令，模組作者可以撤回有問題的已發布版本。
- `go vet` 警告在測試的 goroutine 中呼叫 `t.Fatal` 等方法的錯誤用法。
- 支援 macOS 的 64 位元 ARM（Apple Silicon）；原本的 iOS 平台改名為 `ios/arm64`。

### 執行時與效能

- 新增 `runtime/metrics` 套件，以穩定的介面讀取執行時的各項指標。
- 設定 `GODEBUG=inittrace=1` 會印出每個套件 `init` 的耗時與配置量，方便找出啟動瓶頸。
- Linux 上改回立即歸還記憶體（`MADV_DONTNEED`），RSS 等統計更能反映實際用量。
- 連結器延續 1.15 的改寫並擴及所有平台，連結快 20%–25%；編譯器能內嵌含 `for` 迴圈的函式。

### 標準庫重點

- 新增 `embed` 套件，搭配 `//go:embed` 把檔案嵌入成 `string`、`[]byte` 或 `embed.FS`。
- 新增 `io/fs` 套件與 `fs.FS` 介面；`os.DirFS`、`embed.FS`、`zip.Reader` 都實作它，`http.FS` 與樣板的 `ParseFS` 則能讀取它。
- 新增 `testing/fstest`，提供記憶體中的 `MapFS` 方便測試。
- `io/ioutil` 的功能搬到 `io` 與 `os`，例如 `os.ReadFile`、`os.WriteFile`、`os.ReadDir`、`io.ReadAll`。
- `os/signal` 新增 `NotifyContext`，收到指定訊號時自動取消 context。

### //go:embed

在套件層級變數上方寫 `//go:embed 檔名`，編譯時就會把檔案內容放進變數。要使用 `embed.FS` 或 `//go:embed` 必須匯入 `embed` 套件。執行前先在同一目錄建立 `hello.txt`，內容為 `hello from embedded file`。

```go
package main

import (
	"embed"
	"fmt"
)

//go:embed hello.txt
var hello string

//go:embed hello.txt
var files embed.FS

func main() {
	fmt.Print(hello)

	data, err := files.ReadFile("hello.txt")
	if err != nil {
		fmt.Println("error:", err)
		return
	}
	fmt.Println(len(data))
}
```

```text
hello from embedded file
25
```

### io/fs 與 fstest.MapFS

只要接受 `fs.FS` 介面，同一段程式就能處理真實目錄、嵌入檔案或測試用的記憶體檔案系統。`fs.WalkDir` 依字典順序走訪，所以輸出固定。

```go
package main

import (
	"fmt"
	"io/fs"
	"testing/fstest"
)

func main() {
	fsys := fstest.MapFS{
		"docs/a.md":   {Data: []byte("doc A")},
		"docs/b.md":   {Data: []byte("doc B")},
		"config.json": {Data: []byte("{}")},
	}

	fs.WalkDir(fsys, ".", func(path string, d fs.DirEntry, err error) error {
		if err != nil {
			return err
		}
		if !d.IsDir() {
			fmt.Println(path)
		}
		return nil
	})

	data, _ := fs.ReadFile(fsys, "docs/a.md")
	fmt.Println(string(data))
}
```

```text
config.json
docs/a.md
docs/b.md
doc A
```

## 本版新增的棄用 API

以下 API 從 Go 1.16 起標記為棄用（Deprecated），新程式不要再使用；說明欄為官方原文。

| 套件 | 符號 | 官方說明 |
| --- | --- | --- |
| `crypto/dsa` | `(package)` | DSA is a legacy algorithm, and modern alternatives such as Ed25519 (implemented by package crypto/ed25519) should be used instead. Keys with 1024-bit moduli (L1024N160 parameters) are cryptographically weak, while bigger keys are not widely supported. Note that FIPS 186-5 no longer approves DSA for signature generation. |
| `crypto/x509` | `DecryptPEMBlock` | Legacy PEM encryption as specified in RFC 1423 is insecure by design. Since it does not authenticate the ciphertext, it is vulnerable to padding oracle attacks that can let an attacker recover the plaintext. |
| `crypto/x509` | `EncryptPEMBlock` | Legacy PEM encryption as specified in RFC 1423 is insecure by design. Since it does not authenticate the ciphertext, it is vulnerable to padding oracle attacks that can let an attacker recover the plaintext. |
| `crypto/x509` | `IsEncryptedPEMBlock` | Legacy PEM encryption as specified in RFC 1423 is insecure by design. Since it does not authenticate the ciphertext, it is vulnerable to padding oracle attacks that can let an attacker recover the plaintext. |

## 修補版本

| 版本 | 發布日期 | 含安全修補 | 內容（官方原文） |
| --- | --- | --- | --- |
| `go1.16.1` | 2021-03-10 | 是 | includes security fixes to the archive/zip and encoding/xml packages. |
| `go1.16.2` | 2021-03-11 | 否 | includes fixes to cgo, the compiler, linker, the go command, and the syscall and time packages. |
| `go1.16.3` | 2021-04-01 | 否 | includes fixes to the compiler, linker, runtime, the go command, and the testing and time packages. |
| `go1.16.4` | 2021-05-06 | 是 | includes a security fix to the net/http package, as well as bug fixes to the compiler, runtime, and the archive/zip, syscall, and time packages. |
| `go1.16.5` | 2021-06-03 | 是 | includes security fixes to the archive/zip, math/big, net, and net/http/httputil packages, as well as bug fixes to the linker, the go command, and the net/http package. |
| `go1.16.6` | 2021-07-12 | 是 | includes a security fix to the crypto/tls package, as well as bug fixes to the compiler, and the net and net/http packages. |
| `go1.16.7` | 2021-08-05 | 是 | includes a security fix to the net/http/httputil package, as well as bug fixes to the compiler, linker, runtime, the go command, and the net/http package. |
| `go1.16.8` | 2021-09-09 | 是 | includes a security fix to the archive/zip package, as well as bug fixes to the archive/zip, go/internal/gccgoimporter, html/template, net/http, and runtime/pprof packages. |
| `go1.16.9` | 2021-10-07 | 是 | includes security fixes to linker and the misc/wasm directory, as well as bug fixes to runtime and the text/template package. |
| `go1.16.10` | 2021-11-04 | 是 | includes security fixes to the archive/zip and debug/macho packages, as well as bug fixes to the compiler, linker, runtime, the misc/wasm directory, and the net/http package. |
| `go1.16.11` | 2021-12-02 | 否 | includes fixes to the compiler, runtime, and the net/http, net/http/httptest, and time packages. |
| `go1.16.12` | 2021-12-09 | 是 | includes security fixes to the net/http and syscall packages. |
| `go1.16.13` | 2022-01-06 | 否 | includes fixes to the compiler, linker, runtime, and the net/http package. |
| `go1.16.14` | 2022-02-10 | 是 | includes security fixes to the go command, and the crypto/elliptic and math/big packages, as well as bug fixes to the compiler, linker, runtime, the go command, and the debug/macho, debug/pe, net/http/httptest, and testing packages. |
| `go1.16.15` | 2022-03-03 | 是 | includes a security fix to the regexp/syntax package, as well as bug fixes to the compiler, runtime, the go command, and the net package. |

## 已知漏洞

在 Go 1.16 系列修補版中修正的漏洞共 19 個；仍影響最新修補版 `go1.16.15` 的漏洞共 166 個。

### 本系列已修正

| 編號 | CVE | 摘要（官方原文） | 修正於 |
| --- | --- | --- | --- |
| [GO-2022-0318](https://pkg.go.dev/vuln/GO-2022-0318) | CVE-2022-23773 | Incorrect access control in the go command in cmd/go/internal/modfetch | go1.16.14、go1.17.7 |
| [GO-2022-0289](https://pkg.go.dev/vuln/GO-2022-0289) | CVE-2021-44717 | Misdirected I/O in syscall | go1.16.12、go1.17.5 |
| [GO-2022-0288](https://pkg.go.dev/vuln/GO-2022-0288) | CVE-2021-44716 | Unbounded memory growth in net/http and golang.org/x/net/http2 | go1.16.12、go1.17.5 |
| [GO-2022-0273](https://pkg.go.dev/vuln/GO-2022-0273) | CVE-2021-39293 | Panic due to crafted inputs in archive/zip | go1.16.8、go1.17.1 |
| [GO-2022-0247](https://pkg.go.dev/vuln/GO-2022-0247) | CVE-2021-38297 | Buffer overflow in WASM modules in misc/wasm and cmd/link | go1.16.9、go1.17.2 |
| [GO-2022-0236](https://pkg.go.dev/vuln/GO-2022-0236) | CVE-2021-31525 | Panic due to large headers in net/http and golang.org/x/net/http/httpguts | go1.15.12、go1.16.4 |
| [GO-2021-0347](https://pkg.go.dev/vuln/GO-2021-0347) | CVE-2022-24921 | Stack exhaustion when compiling deeply nested expressions in regexp | go1.16.15、go1.17.8 |
| [GO-2021-0319](https://pkg.go.dev/vuln/GO-2021-0319) | CVE-2022-23806 | Incorrect computation for some invalid field elements in crypto/elliptic | go1.16.14、go1.17.7 |
| [GO-2021-0317](https://pkg.go.dev/vuln/GO-2021-0317) | CVE-2022-23772 | Uncontrolled memory consumption in math/big | go1.16.14、go1.17.7 |
| [GO-2021-0264](https://pkg.go.dev/vuln/GO-2021-0264) | CVE-2021-41772 | Panic when opening certain archives in archive/zip | go1.16.10、go1.17.3 |
| [GO-2021-0263](https://pkg.go.dev/vuln/GO-2021-0263) | CVE-2021-41771 | Panic on invalid symbol tables in debug/macho | go1.16.10、go1.17.3 |
| [GO-2021-0245](https://pkg.go.dev/vuln/GO-2021-0245) | CVE-2021-36221 | Panic in ReverseProxy in net/http/httputil | go1.15.15、go1.16.7 |
| [GO-2021-0243](https://pkg.go.dev/vuln/GO-2021-0243) | CVE-2021-34558 | Panic on certain certificates in crypto/tls | go1.15.14、go1.16.6 |
| [GO-2021-0242](https://pkg.go.dev/vuln/GO-2021-0242) | CVE-2021-33198 | Panic on inputs with large exponents in math/big | go1.15.13、go1.16.5 |
| [GO-2021-0241](https://pkg.go.dev/vuln/GO-2021-0241) | CVE-2021-33197 | Attacker can drop certain headers in net/http/httputil | go1.15.13、go1.16.5 |
| [GO-2021-0240](https://pkg.go.dev/vuln/GO-2021-0240) | CVE-2021-33196 | Panic when reading certain archives in archive/zip | go1.15.13、go1.16.5 |
| [GO-2021-0239](https://pkg.go.dev/vuln/GO-2021-0239) | CVE-2021-33195 | Improper sanitization when resolving values from DNS in net | go1.15.13、go1.16.5 |
| [GO-2021-0234](https://pkg.go.dev/vuln/GO-2021-0234) | CVE-2021-27918 | Infinite loop when decoding inputs in encoding/xml | go1.15.9、go1.16.1 |
| [GO-2021-0067](https://pkg.go.dev/vuln/GO-2021-0067) | CVE-2021-27919 | Panic when opening archives in archive/zip | go1.16.1 |

本版已停止支援，不會再修正這些漏洞；完整清單見 [資安漏洞](/security)。


## 資料來源

- [Go 1.16 Release Notes](https://go.dev/doc/go1.16)
- [Go 發版歷史](https://go.dev/doc/devel/release#go1.16)
- [Go 漏洞資料庫](https://vuln.go.dev)
- [golang/go 原始碼 go1.16](https://github.com/golang/go/tree/go1.16)
