Go 1.17

最後更新


Go 1.17 發布於 2021-08-16,目前最新修補版為 go1.17.13,已停止支援,請升級到 go1.27.4。

版本介紹


Go 1.17 在 64 位元 x86(Linux、macOS、Windows)上改用暫存器傳遞函式參數與回傳值,程式平均快約 5%、執行檔小約 2%,不必改任何程式碼。模組方面引入「模組圖修剪(module graph pruning)」,go.mod 會列出所有間接依賴,go 命令因此只需讀取真正用到的模組。語言上則新增切片轉陣列指標的轉換。

語言變更


  • 切片可以轉換成陣列指標:(*[N]T)(s),結果與切片共用底層陣列;切片長度小於 N 時會 panic,這是第一個可能在執行時失敗的型別轉換。
  • 新增 unsafe.Add(ptr, len) 做指標位移,以及 unsafe.Slice(ptr, len) 從指標建立切片。

工具


  • go.mod 宣告 go 1.17 以上時啟用模組圖修剪與延遲載入,間接依賴另放在一個 require 區塊;可用 go mod tidy -go=1.17 升級。
  • 模組作者可在 go.mod 加上 // Deprecated: 註記棄用整個模組,go get 會顯示警告。
  • 建置限制改用 //go:build 布林運算式,取代 // +build,gofmt 會自動同步兩種寫法。
  • go run example.com/cmd@v1.0.0 可以直接執行指定版本的程式,不必先安裝。
  • go vet 新增檢查://go:build 與 // +build 不一致、signal.Notify 使用無緩衝通道、Is/As/Unwrap 方法簽名錯誤。

執行時與效能


  • 以暫存器為基礎的呼叫慣例(register-based calling convention),效能提升約 5%。
  • 未捕捉的 panic 堆疊追蹤會逐一列出每個參數,比以前的十六進位記憶體內容好讀。
  • 含有閉包(closure)的函式也可以被內嵌。

標準庫重點


  • time 新增 Time.UnixMilli、Time.UnixMicro、time.UnixMilli、time.UnixMicro,以及 Time.IsDST 與 Layout 常數。
  • testing 新增 T.Setenv(測試期間設定環境變數)與 -shuffle 旗標(打亂測試執行順序)。
  • net/url 與 net/http 不再接受分號 ; 作為查詢參數分隔符號。
  • reflect 新增 Value.CanConvert,可先確認轉換是否會成功;sync/atomic 的 Value 新增 Swap、CompareAndSwap。
  • runtime/cgo 新增 Handle,安全地在 Go 與 C 之間傳遞 Go 值。

切片轉陣列指標


(*[2]int)(s) 取得指向切片前兩個元素的陣列指標,透過它修改會反映到原切片。長度不足時會 panic,範例用 recover 接住並印出訊息。
  1. package main
  2. import "fmt"
  3. func main() {
  4. s := []int{1, 2, 3, 4}
  5. p := (*[2]int)(s)
  6. p[0] = 100
  7. fmt.Println(s, *p)
  8. defer func() {
  9. fmt.Println("recovered:", recover())
  10. }()
  11. _ = (*[8]int)(s)
  12. }

  1. [100 2 3 4] [100 2]
  2. recovered: runtime error: cannot convert slice with length 4 to array or pointer to array with length 8

毫秒與微秒時間戳


過去要取得毫秒時間戳得寫 t.UnixNano() / 1e6,Go 1.17 起直接用 UnixMilli;反向轉換則用 time.UnixMilli,回傳的時間位於本地時區,範例中轉回 UTC 以固定輸出。
  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. func main() {
  7. t := time.Date(2021, 8, 16, 0, 0, 0, 500_000_000, time.UTC)
  8. fmt.Println(t.UnixMilli())
  9. fmt.Println(t.UnixMicro())
  10. back := time.UnixMilli(t.UnixMilli()).UTC()
  11. fmt.Println(back.Format(time.RFC3339Nano))
  12. }

  1. 1629072000500
  2. 1629072000500000
  3. 2021-08-16T00:00:00.5Z

本版新增的棄用 API


以下 API 從 Go 1.17 起標記為棄用(Deprecated),新程式不要再使用;說明欄為官方原文。
套件符號官方說明
reflectValue.InterfaceDataThe memory representation of interface values is not compatible with InterfaceData.
syscallCreateIoCompletionPortCreateIoCompletionPort has the wrong function signature. Use x/sys/windows.CreateIoCompletionPort.
syscallGetQueuedCompletionStatusGetQueuedCompletionStatus has the wrong function signature. Use x/sys/windows.GetQueuedCompletionStatus.
syscallPostQueuedCompletionStatusPostQueuedCompletionStatus has the wrong function signature. Use x/sys/windows.PostQueuedCompletionStatus.

修補版本


版本發布日期含安全修補內容(官方原文)
go1.17.12021-09-09是includes a security fix to the archive/zip package, as well as bug fixes to the compiler, linker, the go command, and the crypto/rand, embed, go/types, html/template, and net/http packages.
go1.17.22021-10-07是includes security fixes to linker and the misc/wasm directory, as well as bug fixes to the compiler, runtime, the go command, and the text/template and time packages.
go1.17.32021-11-04是includes security fixes to the archive/zip and debug/macho packages, as well as bug fixes to the compiler, linker, runtime, the go command, the misc/wasm directory, and the net/http and syscall packages.
go1.17.42021-12-02否includes fixes to the compiler, linker, runtime, and the go/types, net/http, and time packages.
go1.17.52021-12-09是includes security fixes to the net/http and syscall packages.
go1.17.62022-01-06否includes fixes to the compiler, linker, runtime, and the crypto/x509, net/http, and reflect packages.
go1.17.72022-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, and net/http/httptest packages.
go1.17.82022-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 crypto/x509 and net packages.
go1.17.92022-04-12是includes security fixes to the crypto/elliptic and encoding/pem packages, as well as bug fixes to the linker and runtime.
go1.17.102022-05-10是includes security fixes to the syscall package, as well as bug fixes to the compiler, runtime, and the crypto/x509 and net/http/httptest packages.
go1.17.112022-06-01是includes security fixes to the crypto/rand, crypto/tls, os/exec, and path/filepath packages, as well as bug fixes to the crypto/tls package.
go1.17.122022-07-12是includes security fixes to the compress/gzip, encoding/gob, encoding/xml, go/parser, io/fs, net/http, and path/filepath packages, as well as bug fixes to the compiler, the go command, the runtime, and the runtime/metrics package.
go1.17.132022-08-01是includes security fixes to the encoding/gob and math/big packages, as well as bug fixes to the compiler and the runtime.

已知漏洞


在 Go 1.17 系列修補版中修正的漏洞共 27 個;仍影響最新修補版 go1.17.13 的漏洞共 149 個。

本系列已修正


編號CVE摘要(官方原文)修正於
GO-2022-0537CVE-2022-32189Panic when decoding Float and Rat types in math/biggo1.17.13、go1.18.5
GO-2022-0533CVE-2022-29804Path traversal via Clean on Windows in path/filepathgo1.17.11、go1.18.3
GO-2022-0532CVE-2022-30580Empty Cmd.Path can trigger unintended binary in os/exec on Windowsgo1.17.11、go1.18.3
GO-2022-0531CVE-2022-30629Session tickets lack random ticket_age_add in crypto/tlsgo1.17.11、go1.18.3
GO-2022-0527CVE-2022-30630Stack exhaustion in Glob on certain paths in io/fsgo1.17.12、go1.18.4
GO-2022-0526CVE-2022-30635Stack exhaustion when decoding certain messages in encoding/gobgo1.17.12、go1.18.4
GO-2022-0525CVE-2022-1705Improper sanitization of Transfer-Encoding headers in net/httpgo1.17.12、go1.18.4
GO-2022-0524CVE-2022-30631Stack exhaustion when reading certain archives in compress/gzipgo1.17.12、go1.18.4
GO-2022-0523CVE-2022-30633Stack exhaustion when unmarshaling certain documents in encoding/xmlgo1.17.12、go1.18.4
GO-2022-0522CVE-2022-30632Stack exhaustion on crafted paths in path/filepathgo1.17.12、go1.18.4
GO-2022-0521CVE-2022-28131Stack exhaustion from deeply nested XML documents in encoding/xmlgo1.17.12、go1.18.4
GO-2022-0520CVE-2022-32148Exposure of client IP addresses in net/httpgo1.17.12、go1.18.4
GO-2022-0515CVE-2022-1962Stack exhaustion due to deeply nested types in go/parsergo1.17.12、go1.18.4
GO-2022-0493CVE-2022-29526Incorrect privilege reporting in syscall and golang.org/x/sys/unixgo1.17.10、go1.18.2
GO-2022-0477CVE-2022-30634Indefinite hang with large buffers on Windows in crypto/randgo1.17.11、go1.18.3
GO-2022-0435CVE-2022-28327Panic due to large inputs affecting P-256 curves in crypto/ellipticgo1.17.9、go1.18.1
GO-2022-0433CVE-2022-24675Stack overflow from a large amount of PEM data in encoding/pemgo1.17.9、go1.18.1
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-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

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

資料來源