Go 1.1
最後更新
Go 1.1 發布於 2013-05-13,目前最新修補版為
go1.1.2,已停止支援,請升級到 go1.27.4。版本介紹
Go 1.1 是 Go 1 之後第一個大改版,主軸是效能:官方測試中多數程式只要重新編譯就快了約 30%–40%。語言上加入了「方法值(method value)」,工具上則首次內建競態偵測器(race detector),從此並行程式的資料競爭(data race)可以用
-race 自動抓出來。語言變更
- 新增方法值:
x.Method可以當成一個已綁定接收者(receiver)的函式值傳來傳去。 - 放寬結尾
return規則:只要函式最後一個敘述必定不會往下執行(例如無條件的for迴圈、兩邊都return的if-else),就不必再補return。 - 除以常數 0 的整數運算改為編譯錯誤,不再等到執行時才 panic。
- Unicode 代理字元(surrogate half)不再是合法的
rune或字串常數。
工具
- 新增競態偵測器,以
go test -race、go build -race啟用。 go test新增-blockprofile,分析 goroutine 卡在哪裡等待;開啟效能分析時會保留測試執行檔方便後續分析。
執行時與效能
- 在 64 位元平台上
int、uint改為 64 位元,切片可以超過 20 億個元素。 - 64 位元平台的堆積(heap)上限從數 GB 提高到數十 GB。
- 新的映射實作大幅降低記憶體與 CPU 用量;垃圾回收(GC)更平行、更精確。
- 執行時與網路函式庫整合得更緊密,網路操作需要的執行緒切換更少。
標準庫重點
bufio新增Scanner,用簡單的迴圈逐行或逐字讀取輸入。- 新增
go/format(以程式呼叫gofmt的格式化)與net/http/cookiejar(管理 HTTP cookie)。 reflect新增Select、MakeFunc、Value.Convert等能力。time在 Linux、macOS 等系統上提升到奈秒精度,並新增Round、Truncate方法。
方法值
把
c.Inc 存進變數後,它就「記住」了 c,之後呼叫時不必再提供接收者。- package main
- import (
- "fmt"
- "strings"
- )
- type Counter struct {
- n int
- }
- func (c *Counter) Inc() {
- c.n++
- }
- func repeat(times int, f func()) {
- for i := 0; i < times; i++ {
- f()
- }
- }
- func main() {
- c := &Counter{}
- inc := c.Inc
- repeat(3, inc)
- fmt.Println(c.n)
- upper := strings.ToUpper
- fmt.Println(upper("method value"))
- }
- 3
- METHOD VALUE
bufio.Scanner
Scanner 預設逐行切割並去掉換行字元;換成 bufio.ScanWords 就能逐字讀取。讀完後記得檢查 Err()。- package main
- import (
- "bufio"
- "fmt"
- "strings"
- )
- func main() {
- input := "first line\nsecond line\nthird line\n"
- scanner := bufio.NewScanner(strings.NewReader(input))
- n := 0
- for scanner.Scan() {
- n++
- fmt.Printf("%d: %s\n", n, scanner.Text())
- }
- if err := scanner.Err(); err != nil {
- fmt.Println("error:", err)
- }
- words := bufio.NewScanner(strings.NewReader("go is fun"))
- words.Split(bufio.ScanWords)
- count := 0
- for words.Scan() {
- count++
- }
- fmt.Println("words:", count)
- }
- 1: first line
- 2: second line
- 3: third line
- words: 3
本版新增的棄用 API
本版沒有新增棄用 API。
修補版本
| 版本 | 發布日期 | 含安全修補 | 內容(官方原文) |
|---|---|---|---|
go1.1.1 | 2013-06-13 | 是 | includes a security fix to the compiler and several bug fixes to the compiler and runtime. |
go1.1.2 | 2013-08-13 | 否 | includes fixes to the gc compiler and cgo, and the bufio, runtime, syscall, and time packages. |
已知漏洞
在 Go 1.1 系列修補版中修正的漏洞共 0 個;仍影響最新修補版
go1.1.2 的漏洞共 214 個。本版已停止支援,不會再修正這些漏洞;完整清單見 資安漏洞。