Go 1.9

最後更新


Go 1.9 發布於 2017-08-24,目前最新修補版為 go1.9.7,已停止支援,請升級到 go1.27.4。

版本介紹


Go 1.9 加入型別別名(type alias),讓大型專案能把型別從一個套件逐步搬到另一個套件而不破壞既有程式。標準庫則帶來好幾個常用工具:sync.Map 並行安全映射、math/bits 位元運算、測試輔助用的 t.Helper,而 time 也開始透明地使用單調時鐘(monotonic clock)計算時間差。

語言變更


  • 新增型別別名 type T1 = T2:T1 只是 T2 的另一個名字,兩者是完全相同的型別。
  • 規格明定實作可以把浮點運算融合(例如用 FMA 指令計算 x*y + z);要強制中間結果先捨入,寫成 float64(x*y) + z。

工具


  • 編譯器可以平行編譯同一個套件內的函式,善用多核心。
  • ./... 不再比對 vendor 目錄內的套件,要包含時寫 ./vendor/...。
  • Go 安裝目錄搬移後,go 工具仍能自動找到正確的 GOROOT。
  • go env -json 以 JSON 輸出;go test -list 只列出符合的測試名稱而不執行。

執行時與效能


  • runtime.GC、debug.SetGCPercent、debug.FreeOSMemory 改為觸發並行垃圾回收,只阻塞呼叫者。
  • 大型堆積(超過 50GB)中配置大物件的效能明顯改善;runtime.ReadMemStats 在大堆積下也低於 100 微秒。
  • 呼叫堆疊可能含有內嵌(inline)的函式框,取得呼叫資訊應使用 runtime.CallersFrames。

標準庫重點


  • time.Time 內含單調時鐘讀數,time.Since、Sub 等計算不受系統時間被調整的影響。
  • 新增 math/bits,提供計算位元數、前導零、反轉等函式,多數架構上會被編譯成單一 CPU 指令。
  • testing 新增 T.Helper、B.Helper,失敗訊息會指向呼叫輔助函式的那一行。
  • sync 新增 Map,適合「寫入少、讀取多」或「各 goroutine 存取不同鍵」的並行情境。
  • runtime/pprof 支援替效能分析資料加上標籤(label)。

型別別名


Temperature 只是 Celsius 的別名,所以可以直接傳給接受 Celsius 的函式,%T 印出的也是原本的型別名稱。這與 type Temperature Celsius(定義一個全新型別)不同。
  1. package main
  2. import "fmt"
  3. type Celsius float64
  4. type Temperature = Celsius
  5. func describe(c Celsius) string {
  6. return fmt.Sprintf("%.1f°C", float64(c))
  7. }
  8. func main() {
  9. var t Temperature = 36.5
  10. fmt.Println(describe(t))
  11. fmt.Printf("%T\n", t)
  12. type Strings = []string
  13. var s Strings = []string{"a", "b"}
  14. fmt.Println(len(s))
  15. }

  1. 36.5°C
  2. main.Celsius
  3. 2

math/bits


0b0010_1100 有 3 個 1、前面有 2 個 0、後面有 2 個 0;bits.Len(1000) 表示 1000 需要 10 個位元。範例中的二進位字面值與底線分隔是 Go 1.13 起的寫法。
  1. package main
  2. import (
  3. "fmt"
  4. "math/bits"
  5. )
  6. func main() {
  7. var x uint8 = 0b0010_1100
  8. fmt.Println(bits.OnesCount8(x))
  9. fmt.Println(bits.LeadingZeros8(x))
  10. fmt.Println(bits.TrailingZeros8(x))
  11. fmt.Println(bits.Len(1000))
  12. fmt.Printf("%08b\n", bits.Reverse8(x))
  13. }

  1. 3
  2. 2
  3. 2
  4. 10
  5. 00110100

本版新增的棄用 API


以下 API 從 Go 1.9 起標記為棄用(Deprecated),新程式不要再使用;說明欄為官方原文。
套件符號官方說明
database/sql/driverColumnConverterDrivers should implement [NamedValueChecker].
runtimeCPUProfileUse the [runtime/pprof] package, or the handlers in the [net/http/pprof] package, or the [testing] package's -test.cpuprofile flag instead.

修補版本


版本發布日期含安全修補內容(官方原文)
go1.9.12017-10-04是includes two security fixes.
go1.9.22017-10-25否includes fixes to the compiler, linker, runtime, documentation, go command, and the crypto/x509, database/sql, log, and net/smtp packages. It includes a fix to a bug introduced in Go 1.9.1 that broke go get of non-Git repositories under certain conditions.
go1.9.32018-01-22是includes security fixes to the net/url package, as well as bug fixes to the compiler, runtime, and the database/sql, math/big, and net/http packages.
go1.9.42018-02-07是includes a security fix to "go get".
go1.9.52018-03-28是includes security fixes to the go command, as well as bug fixes to the compiler, go command, and the net/http/pprof package.
go1.9.62018-05-01否includes fixes to the compiler and go command.
go1.9.72018-06-05否includes fixes to the go command, and the crypto/x509 and strings packages. In particular, it adds minimal support to the go command for the vgo transition.

已知漏洞


在 Go 1.9 系列修補版中修正的漏洞共 4 個;仍影響最新修補版 go1.9.7 的漏洞共 203 個。

本系列已修正


編號CVE摘要(官方原文)修正於
GO-2022-0203CVE-2018-7187Remote command execution via "go get" command with "-insecure" option in cmd/gogo1.9.5、go1.10.1
GO-2022-0201CVE-2018-6574Remote command execution via "go get" command with cgo in cmd/gogo1.8.7、go1.9.4
GO-2022-0177CVE-2017-15041Remote command execution via "go get" in cmd/gogo1.8.4、go1.9.1
GO-2021-0178CVE-2017-15042Cleartext transmission of credentials in net/smtpgo1.8.4、go1.9.1

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

資料來源