Go 1.14

最後更新


Go 1.14 發布於 2020-02-25,目前最新修補版為 go1.14.15,已停止支援,請升級到 go1.27.4。

版本介紹


Go 1.14 宣布模組(module)已可用於正式環境,鼓勵所有專案從 GOPATH 遷移到模組。執行時方面有兩個大進步:goroutine 可以被非同步搶占(asynchronous preemption),沒有函式呼叫的緊密迴圈不再卡住排程器或延誤垃圾回收;defer 的開銷則降到幾乎和直接呼叫一樣,效能敏感的程式碼也能放心使用。

語言變更


  • 介面嵌入允許方法重疊:嵌入的多個介面中有同名、同簽名的方法時不再報錯,解決菱形嵌入的問題;直接宣告的方法仍不可重複。

工具


  • 模組可用於正式環境;go.mod 宣告 go 1.14 以上且有 vendor 目錄時,預設使用 -mod=vendor。
  • 新增 -mod=mod(從模組快取載入)與 -modcacherw(讓模組快取目錄可寫入)旗標。
  • go.mod 唯讀且沒有 vendor 目錄時,預設採用 -mod=readonly。
  • 編譯器新增 -d=checkptr,在執行時檢查 unsafe.Pointer 的使用是否符合規則,-race、-msan 時預設啟用(Windows 除外)。
  • 編譯器可用 -json 輸出內嵌、逃逸分析等最佳化決策的紀錄。
  • 新增實驗性的 64 位元 RISC-V Linux 平台;移除 Native Client(NaCl)支援。

執行時與效能


  • 大多數 defer 幾乎零額外開銷。
  • goroutine 可被非同步搶占;副作用是 Unix 上的程式會收到更多訊號,慢速系統呼叫可能回傳 EINTR。
  • 頁面配置器在高 GOMAXPROCS 下的鎖競爭大幅減少。
  • time.After、time.Tick、net.Conn.SetDeadline 使用的內部計時器更有效率。

標準庫重點


  • 新增 hash/maphash,提供給雜湊表使用的位元組序列雜湊函式(非加密用途,每個行程結果不同)。
  • testing 新增 T.Cleanup、B.Cleanup,註冊在測試結束後執行的清理函式。
  • crypto/tls 移除 SSLv3 支援。
  • math 新增 FMA,計算 x*y+z 且中間不捨入;log 新增 Lmsgprefix 旗標。

重疊的介面嵌入


ReadCloser 與 WriteCloser 都有 Close() error。Go 1.14 之前把兩者嵌入同一個介面會出現「duplicate method」錯誤,現在因為簽名相同而被允許,Close 只算一個方法。
  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. type ReadCloser interface {
  7. Read() string
  8. Close() error
  9. }
  10. type WriteCloser interface {
  11. Write(s string)
  12. Close() error
  13. }
  14. type ReadWriteCloser interface {
  15. ReadCloser
  16. WriteCloser
  17. }
  18. type memFile struct {
  19. sb strings.Builder
  20. closed bool
  21. }
  22. func (f *memFile) Read() string { return f.sb.String() }
  23. func (f *memFile) Write(s string) { f.sb.WriteString(s) }
  24. func (f *memFile) Close() error {
  25. f.closed = true
  26. return nil
  27. }
  28. func main() {
  29. var rwc ReadWriteCloser = &memFile{}
  30. rwc.Write("hello")
  31. fmt.Println(rwc.Read())
  32. rwc.Close()
  33. fmt.Println(rwc.(*memFile).closed)
  34. }

  1. hello
  2. true

本版新增的棄用 API


以下 API 從 Go 1.14 起標記為棄用(Deprecated),新程式不要再使用;說明欄為官方原文。
套件符號官方說明
crypto/tlsConfig.BuildNameToCertificateNameToCertificate only allows associating a single certificate with a given name. Leave that field nil to let the library select the first compatible chain from Certificates.
crypto/tlsConfig.NameToCertificateNameToCertificate only allows associating a single certificate with a given name. Leave this field nil to let the library select the first compatible chain from Certificates.
net/httpTransport.DialTLSUse DialTLSContext instead, which allows the transport to cancel dials as soon as they are no longer needed. If both are set, DialTLSContext takes priority.

修補版本


版本發布日期含安全修補內容(官方原文)
go1.14.12020-03-19否includes fixes to the go command, tools, and the runtime.
go1.14.22020-04-08否includes fixes to cgo, the go command, the runtime, and the os/exec and testing packages.
go1.14.32020-05-14否includes fixes to cgo, the compiler, the runtime, and the go/doc and math/big packages.
go1.14.42020-06-01否includes fixes to the go doc command, the runtime, and the encoding/json and os packages.
go1.14.52020-07-14是includes security fixes to the crypto/x509 and net/http packages.
go1.14.62020-07-16否includes fixes to the go command, the compiler, the linker, vet, and the database/sql, encoding/json, net/http, reflect, and testing packages.
go1.14.72020-08-06是includes security fixes to the encoding/binary package.
go1.14.82020-09-01是includes security fixes to the net/http/cgi and net/http/fcgi packages.
go1.14.92020-09-09否includes fixes to the compiler, linker, runtime, documentation, and the net/http and testing packages.
go1.14.102020-10-14否includes fixes to the compiler, runtime, and the plugin and testing packages.
go1.14.112020-11-05否includes fixes to the runtime, and the net/http and time packages.
go1.14.122020-11-12是includes security fixes to the go command and the math/big package.
go1.14.132020-12-03否includes fixes to the compiler, runtime, and the go command.
go1.14.142021-01-19是includes security fixes to the go command and the crypto/elliptic package.
go1.14.152021-02-04否includes fixes to the compiler, runtime, the go command, and the net/http package.

已知漏洞


在 Go 1.14 系列修補版中修正的漏洞共 9 個;仍影響最新修補版 go1.14.15 的漏洞共 184 個。

本系列已修正


編號CVE摘要(官方原文)修正於
GO-2022-0476CVE-2020-28367Arbitrary code execution via the go command with cgo in cmd/gogo1.14.12、go1.15.5
GO-2022-0475CVE-2020-28366Arbitrary code execution in go command with cgo in cmd/go and cmd/cgogo1.14.12、go1.15.5
GO-2021-0235CVE-2021-3114Incorrect operations on the P-224 curve in crypto/ellipticgo1.14.14、go1.15.7
GO-2021-0226CVE-2020-24553Cross-site scripting in net/http/cgi and net/http/fcgigo1.14.8、go1.15.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
GO-2021-0069CVE-2020-28362Panic during division of very large numbers in math/biggo1.14.12、go1.15.5
GO-2021-0068CVE-2021-3115Arbitrary code injection via the go command with cgo on Windows in cmd/gogo1.14.14、go1.15.7

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

資料來源