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。
  1. package main
  2. import (
  3. "embed"
  4. "fmt"
  5. )
  6. //go:embed hello.txt
  7. var hello string
  8. //go:embed hello.txt
  9. var files embed.FS
  10. func main() {
  11. fmt.Print(hello)
  12. data, err := files.ReadFile("hello.txt")
  13. if err != nil {
  14. fmt.Println("error:", err)
  15. return
  16. }
  17. fmt.Println(len(data))
  18. }

  1. hello from embedded file
  2. 25

io/fs 與 fstest.MapFS


只要接受 fs.FS 介面,同一段程式就能處理真實目錄、嵌入檔案或測試用的記憶體檔案系統。fs.WalkDir 依字典順序走訪,所以輸出固定。
  1. package main
  2. import (
  3. "fmt"
  4. "io/fs"
  5. "testing/fstest"
  6. )
  7. func main() {
  8. fsys := fstest.MapFS{
  9. "docs/a.md": {Data: []byte("doc A")},
  10. "docs/b.md": {Data: []byte("doc B")},
  11. "config.json": {Data: []byte("{}")},
  12. }
  13. fs.WalkDir(fsys, ".", func(path string, d fs.DirEntry, err error) error {
  14. if err != nil {
  15. return err
  16. }
  17. if !d.IsDir() {
  18. fmt.Println(path)
  19. }
  20. return nil
  21. })
  22. data, _ := fs.ReadFile(fsys, "docs/a.md")
  23. fmt.Println(string(data))
  24. }

  1. config.json
  2. docs/a.md
  3. docs/b.md
  4. 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/x509DecryptPEMBlockLegacy 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/x509EncryptPEMBlockLegacy 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/x509IsEncryptedPEMBlockLegacy 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.12021-03-10是includes security fixes to the archive/zip and encoding/xml packages.
go1.16.22021-03-11否includes fixes to cgo, the compiler, linker, the go command, and the syscall and time packages.
go1.16.32021-04-01否includes fixes to the compiler, linker, runtime, the go command, and the testing and time packages.
go1.16.42021-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.52021-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.62021-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.72021-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.82021-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.92021-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.102021-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.112021-12-02否includes fixes to the compiler, runtime, and the net/http, net/http/httptest, and time packages.
go1.16.122021-12-09是includes security fixes to the net/http and syscall packages.
go1.16.132022-01-06否includes fixes to the compiler, linker, runtime, and the net/http package.
go1.16.142022-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.152022-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-0318CVE-2022-23773Incorrect access control in the go command in cmd/go/internal/modfetchgo1.16.14、go1.17.7
GO-2022-0289CVE-2021-44717Misdirected I/O in syscallgo1.16.12、go1.17.5
GO-2022-0288CVE-2021-44716Unbounded memory growth in net/http and golang.org/x/net/http2go1.16.12、go1.17.5
GO-2022-0273CVE-2021-39293Panic due to crafted inputs in archive/zipgo1.16.8、go1.17.1
GO-2022-0247CVE-2021-38297Buffer overflow in WASM modules in misc/wasm and cmd/linkgo1.16.9、go1.17.2
GO-2022-0236CVE-2021-31525Panic due to large headers in net/http and golang.org/x/net/http/httpgutsgo1.15.12、go1.16.4
GO-2021-0347CVE-2022-24921Stack exhaustion when compiling deeply nested expressions in regexpgo1.16.15、go1.17.8
GO-2021-0319CVE-2022-23806Incorrect computation for some invalid field elements in crypto/ellipticgo1.16.14、go1.17.7
GO-2021-0317CVE-2022-23772Uncontrolled memory consumption in math/biggo1.16.14、go1.17.7
GO-2021-0264CVE-2021-41772Panic when opening certain archives in archive/zipgo1.16.10、go1.17.3
GO-2021-0263CVE-2021-41771Panic on invalid symbol tables in debug/machogo1.16.10、go1.17.3
GO-2021-0245CVE-2021-36221Panic in ReverseProxy in net/http/httputilgo1.15.15、go1.16.7
GO-2021-0243CVE-2021-34558Panic on certain certificates in crypto/tlsgo1.15.14、go1.16.6
GO-2021-0242CVE-2021-33198Panic on inputs with large exponents in math/biggo1.15.13、go1.16.5
GO-2021-0241CVE-2021-33197Attacker can drop certain headers in net/http/httputilgo1.15.13、go1.16.5
GO-2021-0240CVE-2021-33196Panic when reading certain archives in archive/zipgo1.15.13、go1.16.5
GO-2021-0239CVE-2021-33195Improper sanitization when resolving values from DNS in netgo1.15.13、go1.16.5
GO-2021-0234CVE-2021-27918Infinite loop when decoding inputs in encoding/xmlgo1.15.9、go1.16.1
GO-2021-0067CVE-2021-27919Panic when opening archives in archive/zipgo1.16.1

本版已停止支援,不會再修正這些漏洞;完整清單見 資安漏洞。

資料來源