Go 1.13

最後更新


Go 1.13 發布於 2019-09-03,目前最新修補版為 go1.13.15,已停止支援,請升級到 go1.27.4。

版本介紹


Go 1.13 有兩件影響深遠的事:一是錯誤包裝(error wrapping),fmt.Errorf 的 %w 搭配 errors.Is、errors.As,讓錯誤可以一層層加上說明又不失去原本的身分;二是 go 命令預設透過 Google 營運的模組鏡像(proxy.golang.org)與校驗和資料庫(sum.golang.org)下載並驗證模組。語言上也加入了更現代的數字字面值寫法。

語言變更


  • 新增二進位字面值 0b1011、八進位字面值 0o660,以及十六進位浮點數 0x1p-2。
  • 數字可以用底線分隔,例如 1_000_000、0b_1010_0110。
  • 虛數後綴 i 可用在任何整數或浮點數字面值。
  • 位移運算的位移量可以是有號整數,不必再轉成 uint;位移量為負數時會 panic。
  • 使用模組時,go.mod 的 go 版本需至少為 1.13 才能用這些語法。

工具


  • GOPROXY 預設為 https://proxy.golang.org,direct,新增 GOSUMDB 驗證模組校驗和、GOPRIVATE 標示不公開的模組路徑。
  • GO111MODULE=auto 時,只要所在目錄或上層有 go.mod 就啟用模組模式,即使位於 GOPATH/src 內。
  • go env -w 可以設定使用者層級的預設環境變數。
  • go version 可以查詢執行檔是用哪個 Go 版本建置;go build -trimpath 移除執行檔中的檔案路徑。
  • 新的逃逸分析(escape analysis)更精確,更多變數能配置在堆疊上。

執行時與效能


  • 索引越界的 panic 訊息會帶出索引與長度,例如 index out of range [3] with length 1。
  • 大多數 defer 的效能提升約 30%。
  • 堆積縮小後,執行時會更快把記憶體還給作業系統。

標準庫重點


  • 錯誤包裝:fmt.Errorf 新增 %w,errors 套件新增 Unwrap、Is、As。
  • crypto/tls 預設啟用 TLS 1.3。
  • 新增 crypto/ed25519,實作 Ed25519 簽章。
  • os 新增 UserConfigDir;sync.Pool 在垃圾回收後會保留部分物件,不再每次全部清空;sync.Mutex 與 Once.Do 的快速路徑被內嵌,未競爭時更快。

新的數字字面值


底線只是給人看的分隔,不影響數值;0o755 的十進位是 493。位移量 shift 是有號的 int,Go 1.13 起可以直接使用。
  1. package main
  2. import "fmt"
  3. func main() {
  4. population := 23_400_000
  5. mask := 0b1111_0000
  6. perm := 0o755
  7. color := 0xFF_88_00
  8. fmt.Println(population)
  9. fmt.Println(mask, perm, color)
  10. fmt.Println(0x1p-2)
  11. shift := -1 + 3
  12. fmt.Println(1 << shift)
  13. }

  1. 23400000
  2. 240 493 16746496
  3. 0.25
  4. 4

錯誤包裝


%w 把底層錯誤包起來;errors.Is 沿著包裝鏈檢查是否為某個錯誤值,errors.As 則找出鏈上第一個符合型別的錯誤並取出來。
  1. package main
  2. import (
  3. "errors"
  4. "fmt"
  5. "os"
  6. )
  7. type QueryError struct {
  8. Query string
  9. }
  10. func (e *QueryError) Error() string {
  11. return "bad query: " + e.Query
  12. }
  13. func loadConfig(path string) error {
  14. _, err := os.Open(path)
  15. if err != nil {
  16. return fmt.Errorf("load config %q: %w", path, err)
  17. }
  18. return nil
  19. }
  20. func search(q string) error {
  21. return fmt.Errorf("search failed: %w", &QueryError{Query: q})
  22. }
  23. func main() {
  24. err := loadConfig("/no/such/file.json")
  25. fmt.Println(errors.Is(err, os.ErrNotExist))
  26. fmt.Println(errors.Unwrap(err) != nil)
  27. var qe *QueryError
  28. if errors.As(search("a=?"), &qe) {
  29. fmt.Println("query:", qe.Query)
  30. }
  31. }

  1. true
  2. true
  3. query: a=?

本版新增的棄用 API


以下 API 從 Go 1.13 起標記為棄用(Deprecated),新程式不要再使用;說明欄為官方原文。
套件符號官方說明
crypto/tlsVersionSSL30SSLv3 is cryptographically broken, and is no longer supported by this package. See golang.org/issue/32716.
debug/gosymLineTable.LineToPCUse Table's LineToPC method instead.
debug/gosymLineTable.PCToLineUse Table's PCToLine method instead.
imageZPUse a literal [image.Point] instead.
imageZRUse a literal [image.Rectangle] instead.

修補版本


版本發布日期含安全修補內容(官方原文)
go1.13.12019-09-25是includes security fixes to the net/http and net/textproto packages.
go1.13.22019-10-17是includes security fixes to the compiler and the crypto/dsa package.
go1.13.32019-10-17否includes fixes to the go command, the toolchain, the runtime, and the crypto/ecdsa, net, net/http, and syscall packages.
go1.13.42019-10-31否includes fixes to the net/http and syscall packages. It also fixes an issue on macOS 10.15 Catalina where the non-notarized installer and binaries were being rejected by Gatekeeper.
go1.13.52019-12-04否includes fixes to the go command, the runtime, the linker, and the net/http package.
go1.13.62020-01-09否includes fixes to the runtime and the net/http package.
go1.13.72020-01-28是includes two security fixes to the crypto/x509 package.
go1.13.82020-02-12否includes fixes to the runtime, and the crypto/x509 and net/http packages.
go1.13.92020-03-19否includes fixes to the go command, tools, the runtime, the toolchain, and the crypto/cypher package.
go1.13.102020-04-08否includes fixes to the go command, the runtime, and the os/exec and time packages.
go1.13.112020-05-14否includes fixes to the compiler.
go1.13.122020-06-01否includes fixes to the runtime, and the go/types and math/big packages.
go1.13.132020-07-14是includes security fixes to the crypto/x509 and net/http packages.
go1.13.142020-07-16否includes fixes to the compiler, vet, and the database/sql, net/http, and reflect packages.
go1.13.152020-08-06是includes security fixes to the encoding/binary package.

已知漏洞


在 Go 1.13 系列修補版中修正的漏洞共 7 個;仍影響最新修補版 go1.13.15 的漏洞共 189 個。

本系列已修正


編號CVE摘要(官方原文)修正於
GO-2022-0535CVE-2020-0601Certificate validation bypass on Windows in crypto/x509go1.12.16、go1.13.7
GO-2022-0229CVE-2020-7919Panic in certificate parsing in crypto/x509 and golang.org/x/crypto/cryptobytego1.12.16、go1.13.7
GO-2022-0213CVE-2019-17596Panic on invalid DSA public keys in crypto/dsago1.12.11、go1.13.2
GO-2022-0212CVE-2019-16276Request smuggling due to accepting invalid headers in net/http via net/textprotogo1.12.10、go1.13.1
GO-2021-0224CVE-2020-15586Data race and crash in net/httpgo1.13.13、go1.14.5
GO-2021-0223CVE-2020-14039Certificate verification error on Windows in crypto/x509go1.13.13、go1.14.5
GO-2021-0142CVE-2020-16845Unbounded read from invalid inputs in encoding/binarygo1.13.15、go1.14.7

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

資料來源