Go 1.8

最後更新


Go 1.8 發布於 2017-02-16,目前最新修補版為 go1.8.7,已停止支援,請升級到 go1.27.4。

版本介紹


Go 1.8 讓日常開發更順手:sort.Slice 只要傳一個比較函式就能排序切片,不必再為每種排序寫一個型別;http.Server 加入優雅關機(graceful shutdown)。底層方面,SSA 編譯器後端推廣到所有架構,垃圾回收暫停時間降到通常低於 100 微秒。

語言變更


  • 結構之間做明確型別轉換時會忽略欄位標籤(tag),只差在標籤的兩個結構可以互相轉換。
  • 規格只要求浮點數常數至少支援 16 位元指數(現有編譯器仍支援 32 位元)。

工具


  • GOPATH 未設定時有預設值:Unix 為 $HOME/go,Windows 為 %USERPROFILE%\go。
  • SSA 後端用於所有架構,32 位元 ARM 的 CPU 時間減少 20%–30%;同時換上新的編譯器前端。
  • 新增 go bug 命令,自動帶入系統資訊開啟 GitHub 問題回報。
  • 早期支援外掛(plugin):-buildmode=plugin 搭配 plugin 套件在執行時載入(僅 Linux)。
  • go vet 新增複製鎖陣列、重複 JSON 標籤等檢查。

執行時與效能


  • 移除「停止世界」時的堆疊重新掃描,垃圾回收暫停通常低於 100 微秒、常常只有 10 微秒。
  • defer 的額外開銷減少約一半;cgo 呼叫的開銷也約減半。
  • 映射並行誤用的偵測更完整。

標準庫重點


  • sort 新增 Slice、SliceStable、SliceIsSorted。
  • http.Server 新增 Shutdown(等待進行中的請求完成後關閉)與 Close(立即關閉)。
  • context 支援擴大:database/sql 新增一系列帶 Context 的方法,net.Resolver 的查詢方法也接受 Context。
  • net/http 支援 HTTP/2 Server Push(http.Pusher 介面)。
  • 支援互斥鎖競爭分析:go test -mutexprofile 搭配 pprof。

sort.Slice


傳入切片與一個 less(i, j) 函式即可排序。需要「相等元素維持原順序」時改用 sort.SliceStable。
  1. package main
  2. import (
  3. "fmt"
  4. "sort"
  5. )
  6. type User struct {
  7. Name string
  8. Age int
  9. }
  10. func main() {
  11. users := []User{
  12. {"Carol", 35},
  13. {"Alice", 28},
  14. {"Bob", 28},
  15. }
  16. sort.Slice(users, func(i, j int) bool {
  17. if users[i].Age != users[j].Age {
  18. return users[i].Age < users[j].Age
  19. }
  20. return users[i].Name < users[j].Name
  21. })
  22. fmt.Println(users)
  23. nums := []int{5, 2, 9}
  24. sort.Slice(nums, func(i, j int) bool { return nums[i] > nums[j] })
  25. fmt.Println(nums)
  26. }

  1. [{Alice 28} {Bob 28} {Carol 35}]
  2. [9 5 2]

忽略標籤的結構轉換


兩個結構的欄位名稱與型別相同、只有標籤不同時,Go 1.8 起可以直接轉換。常見用途是把外部 API 的資料結構轉成自家使用的結構。
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. )
  6. type APIUser struct {
  7. ID int `json:"user_id"`
  8. Name string `json:"user_name"`
  9. }
  10. type User struct {
  11. ID int `json:"id"`
  12. Name string `json:"name"`
  13. }
  14. func main() {
  15. src := APIUser{ID: 1, Name: "gopher"}
  16. dst := User(src)
  17. a, _ := json.Marshal(src)
  18. b, _ := json.Marshal(dst)
  19. fmt.Println(string(a))
  20. fmt.Println(string(b))
  21. }

  1. {"user_id":1,"user_name":"gopher"}
  2. {"id":1,"name":"gopher"}

本版新增的棄用 API


以下 API 從 Go 1.8 起標記為棄用(Deprecated),新程式不要再使用;說明欄為官方原文。
套件符號官方說明
database/sql/driverConn.BeginDrivers should implement ConnBeginTx instead (or additionally).
database/sql/driverExecerDrivers should implement [ExecerContext] instead.
database/sql/driverQueryerDrivers should implement [QueryerContext] instead.
database/sql/driverStmt.ExecDrivers should implement StmtExecContext instead (or additionally).
database/sql/driverStmt.QueryDrivers should implement StmtQueryContext instead (or additionally).
net/httpErrHeaderTooLongErrHeaderTooLong is no longer returned by anything in the net/http package. Callers should not compare errors against this variable.
net/httpErrMissingContentLengthErrMissingContentLength is no longer returned by anything in the net/http package. Callers should not compare errors against this variable.
net/httpErrShortBodyErrShortBody is no longer returned by anything in the net/http package. Callers should not compare errors against this variable.
net/httpProtocolErrorNot all errors in the http package related to protocol errors are of type ProtocolError.
net/http/httputilErrClosedNo longer used.
net/http/httputilErrPersistEOFNo longer used.
net/http/httputilErrPipelineNo longer used.
path/filepathHasPrefixHasPrefix does not respect path boundaries and does not ignore case when required.

修補版本


版本發布日期含安全修補內容(官方原文)
go1.8.12017-04-07否includes fixes to the compiler, linker, runtime, documentation, go command and the crypto/tls, encoding/xml, image/png, net, net/http, reflect, text/template, and time packages.
go1.8.22017-05-23是includes a security fix to the crypto/elliptic package.
go1.8.32017-05-24否includes fixes to the compiler, runtime, documentation, and the database/sql package.
go1.8.42017-10-04是includes two security fixes. It contains the same fixes as Go 1.9.1 and was released at the same time.
go1.8.52017-10-25否includes fixes to the compiler, linker, runtime, documentation, go command, and the crypto/x509 and net/smtp packages. It includes a fix to a bug introduced in Go 1.8.4 that broke go get of non-Git repositories under certain conditions.
go1.8.62018-01-22否includes the same fix in math/big as Go 1.9.3 and was released at the same time.
go1.8.72018-02-07是includes a security fix to "go get". It contains the same fix as Go 1.9.4 and was released at the same time.

已知漏洞


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

本系列已修正


編號CVE摘要(官方原文)修正於
GO-2022-0201CVE-2018-6574Remote command execution via "go get" command with cgo in cmd/gogo1.8.7、go1.9.4
GO-2022-0187CVE-2017-8932Incorrect computation for P-256 curves in crypto/ellipticgo1.7.6、go1.8.2
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

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

資料來源