Go 1.3

最後更新


Go 1.3 發布於 2014-06-18,目前最新修補版為 go1.3.3,已停止支援,請升級到 go1.27.4。

版本介紹


Go 1.3 沒有語言變更,重心在實作:goroutine 堆疊改為連續堆疊(contiguous stack),垃圾回收器對堆疊上的值也變得精確,編譯器工具鏈重構讓大型專案建置更快。標準庫則新增了至今仍常用的 sync.Pool。

工具


  • 連結器的指令選擇階段移到編譯器(liblink),大型專案的建置速度明顯提升。
  • go test 搭配 -race 時,覆蓋率模式自動設為 atomic;沒有測試檔的套件也會被建置。
  • 支援啟用 cgo 的交叉編譯,go run、go test 新增 -exec 選項。
  • godoc 新增 -analysis 靜態分析功能。

執行時與效能


  • goroutine 堆疊從「分段」改為「連續」:空間不夠時整段搬到更大的記憶體,消除在分段邊界反覆切換的效能熱點。
  • 垃圾回收器在掃描堆疊時也變精確,整數不會再被誤認為指標而讓記憶體無法回收。
  • 小映射(8 個元素以下)的走訪順序重新隨機化,揪出依賴走訪順序的錯誤程式。
  • 垃圾回收改用並行清掃(concurrent sweep),暫停時間可減少 50%–70%;defer 的記憶體開銷降低。
  • 記憶體模型(memory model)補充說明:有緩衝的通道(channel)可以當作簡單的號誌(semaphore)使用。

標準庫重點


  • 新增 sync.Pool,可重複利用暫時物件、減輕垃圾回收負擔。
  • testing.B 新增 RunParallel,方便撰寫多 CPU 並行的基準測試。
  • crypto/tls 修正可能意外略過憑證驗證的錯誤:必須設定 ServerName 或 InsecureSkipVerify。
  • regexp 對簡單的表達式加入一次通過(one-pass)引擎,速度明顯提升。

sync.Pool


sync.Pool 像一個「暫存物件回收箱」:用完的物件放回去,下次 Get 時可能直接拿到它,省下重新配置。池裡的物件可能隨時被垃圾回收清掉,所以只適合放可以隨時重建的暫存物件,取出後也要先重設狀態。範例中的 any 是 Go 1.18 起 interface{} 的別名。
  1. package main
  2. import (
  3. "bytes"
  4. "fmt"
  5. "sync"
  6. )
  7. var bufPool = sync.Pool{
  8. New: func() any {
  9. return new(bytes.Buffer)
  10. },
  11. }
  12. func render(name string) string {
  13. buf := bufPool.Get().(*bytes.Buffer)
  14. buf.Reset()
  15. defer bufPool.Put(buf)
  16. buf.WriteString("hello, ")
  17. buf.WriteString(name)
  18. return buf.String()
  19. }
  20. func main() {
  21. fmt.Println(render("gopher"))
  22. fmt.Println(render("pool"))
  23. }

  1. hello, gopher
  2. hello, pool

本版新增的棄用 API


本版沒有新增棄用 API。

修補版本


版本發布日期含安全修補內容(官方原文)
go1.3.12014-08-13否includes bug fixes to the compiler and the runtime, net, and crypto/rsa packages.
go1.3.22014-09-25是includes security fixes to the crypto/tls package and bug fixes to cgo.
go1.3.32014-09-30否includes further bug fixes to cgo, the runtime package, and the nacl port.

已知漏洞


在 Go 1.3 系列修補版中修正的漏洞共 1 個;仍影響最新修補版 go1.3.3 的漏洞共 213 個。

本系列已修正


編號CVE摘要(官方原文)修正於
GO-2021-0154CVE-2014-7189Man-in-the-middle attack with SessionTicketsDisabled in crypto/tlsgo1.3.2

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

資料來源