Go 1.5

最後更新


Go 1.5 發布於 2015-08-19,目前最新修補版為 go1.5.4,已停止支援,請升級到 go1.27.4。

版本介紹


Go 1.5 是架構上的大轉折:編譯器與執行時全部改用 Go 撰寫(只剩少量組合語言),Go 從此「自己編譯自己」(bootstrapping)。同時推出並行垃圾回收器,暫停時間幾乎都壓在 10 毫秒以內;GOMAXPROCS 預設值也從 1 改為 CPU 核心數,程式預設就能用滿多核心。

語言變更


  • 映射字面值(map literal)的鍵可以省略型別名稱,和切片字面值的規則一致。

工具


  • C 編譯器從發行版移除,編譯器與連結器由原本的 C 程式自動翻譯成 Go;建置 Go 本身需要先有 Go 1.4 工具鏈。
  • 編譯器改名為統一的 go tool compile、go tool link、go tool asm,不再依架構區分 6g、8g 等名稱。
  • internal 套件規則對所有專案生效。
  • 實驗性支援 vendoring:設定 GO15VENDOREXPERIMENT=1 後可從專案的 vendor 目錄讀取依賴。
  • 新增 go tool trace,以視覺化方式追蹤程式執行;新增命令列用的 go doc。
  • 新增 -buildmode,可建置共享函式庫(shared library)等不同輸出;go test 新增 -count。

執行時與效能


  • 新的並行垃圾回收器大部分工作與程式同時進行,「停止世界(stop the world)」階段通常遠低於 10 毫秒。
  • GOMAXPROCS 預設為可用 CPU 核心數;goroutine 的排程順序也有改變,依賴排程順序的程式可能受影響。
  • 因編譯器是自動翻譯的 Go 程式碼,此版建置速度約慢了兩倍,後續版本持續改善。

標準庫重點


  • math/big 新增任意精度浮點數型別 Float。
  • go/types 套件從 x/tools 移入標準庫,提供 Go 程式的型別檢查。
  • net 的 DNS 解析在 Unix 上預設使用純 Go 實作,不需要 cgo。
  • flag 的 PrintDefaults 輸出格式改進;reflect 新增 ArrayOf、FuncOf。
  • 以 go-fuzz 隨機測試找出並修正了 fmt、archive/zip、html/template 等數十個標準庫錯誤。

省略型別的映射字面值


鍵是結構型別時,字面值裡可以直接寫 {...},不必重複寫 Point{...};值是切片或結構時本來就能省略。
  1. package main
  2. import "fmt"
  3. type Point struct {
  4. Lat, Lng float64
  5. }
  6. func main() {
  7. places := map[Point]string{
  8. {25.0330, 121.5654}: "台北 101",
  9. {22.6273, 120.3014}: "高雄",
  10. }
  11. fmt.Println(places[Point{25.0330, 121.5654}])
  12. routes := map[string][]Point{
  13. "north": {{25.0, 121.5}, {25.1, 121.6}},
  14. }
  15. fmt.Println(len(routes["north"]))
  16. }

  1. 台北 101
  2. 2

本版新增的棄用 API


以下 API 從 Go 1.5 起標記為棄用(Deprecated),新程式不要再使用;說明欄為官方原文。
套件符號官方說明
archive/zipFileHeader.CompressedSizeUse CompressedSize64 instead.
archive/zipFileHeader.UncompressedSizeUse UncompressedSize64 instead.
go/docPackage.BugsFor backward compatibility Bugs is still populated, but all new code should use Notes instead.
image/jpegReaderReader is not used by the [image/jpeg] package and should not be used by others. It is kept for compatibility.
syscallFormatMessageUse FormatMessage from golang.org/x/sys/windows instead.
syscallStringBytePtrUse [BytePtrFromString] instead.
syscallStringByteSliceUse ByteSliceFromString instead.
syscallStringSlicePtrUse [SlicePtrFromStrings] instead.
syscallStringToUTF16Use [UTF16FromString] instead.
syscallStringToUTF16PtrUse [UTF16PtrFromString] instead.

修補版本


版本發布日期含安全修補內容(官方原文)
go1.5.12015-09-08否includes bug fixes to the compiler, assembler, and the fmt, net/textproto, net/http, and runtime packages.
go1.5.22015-12-02否includes bug fixes to the compiler, linker, and the mime/multipart, net, and runtime packages.
go1.5.32016-01-13是includes a security fix to the math/big package affecting the crypto/tls package.
go1.5.42016-04-12是includes two security fixes. It contains the same fixes as Go 1.6.1 and was released at the same time.

已知漏洞


在 Go 1.5 系列修補版中修正的漏洞共 3 個;仍影響最新修補版 go1.5.4 的漏洞共 210 個。

本系列已修正


編號CVE摘要(官方原文)修正於
GO-2022-0166CVE-2016-3959Denial of service due to unchecked parameters in crypto/dsago1.5.4、go1.6.1
GO-2021-0163CVE-2016-3958Privilege escalation on Windows via malicious DLL in syscallgo1.5.4、go1.6.1
GO-2021-0160CVE-2015-8618Incorrect calculation affecting RSA computations in math/biggo1.5.3

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

資料來源