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。- package main
- import (
- "fmt"
- "sort"
- )
- type User struct {
- Name string
- Age int
- }
- func main() {
- users := []User{
- {"Carol", 35},
- {"Alice", 28},
- {"Bob", 28},
- }
- sort.Slice(users, func(i, j int) bool {
- if users[i].Age != users[j].Age {
- return users[i].Age < users[j].Age
- }
- return users[i].Name < users[j].Name
- })
- fmt.Println(users)
- nums := []int{5, 2, 9}
- sort.Slice(nums, func(i, j int) bool { return nums[i] > nums[j] })
- fmt.Println(nums)
- }
- [{Alice 28} {Bob 28} {Carol 35}]
- [9 5 2]
忽略標籤的結構轉換
兩個結構的欄位名稱與型別相同、只有標籤不同時,Go 1.8 起可以直接轉換。常見用途是把外部 API 的資料結構轉成自家使用的結構。
- package main
- import (
- "encoding/json"
- "fmt"
- )
- type APIUser struct {
- ID int `json:"user_id"`
- Name string `json:"user_name"`
- }
- type User struct {
- ID int `json:"id"`
- Name string `json:"name"`
- }
- func main() {
- src := APIUser{ID: 1, Name: "gopher"}
- dst := User(src)
- a, _ := json.Marshal(src)
- b, _ := json.Marshal(dst)
- fmt.Println(string(a))
- fmt.Println(string(b))
- }
- {"user_id":1,"user_name":"gopher"}
- {"id":1,"name":"gopher"}
本版新增的棄用 API
以下 API 從 Go 1.8 起標記為棄用(Deprecated),新程式不要再使用;說明欄為官方原文。
| 套件 | 符號 | 官方說明 |
|---|---|---|
database/sql/driver | Conn.Begin | Drivers should implement ConnBeginTx instead (or additionally). |
database/sql/driver | Execer | Drivers should implement [ExecerContext] instead. |
database/sql/driver | Queryer | Drivers should implement [QueryerContext] instead. |
database/sql/driver | Stmt.Exec | Drivers should implement StmtExecContext instead (or additionally). |
database/sql/driver | Stmt.Query | Drivers should implement StmtQueryContext instead (or additionally). |
net/http | ErrHeaderTooLong | ErrHeaderTooLong is no longer returned by anything in the net/http package. Callers should not compare errors against this variable. |
net/http | ErrMissingContentLength | ErrMissingContentLength is no longer returned by anything in the net/http package. Callers should not compare errors against this variable. |
net/http | ErrShortBody | ErrShortBody is no longer returned by anything in the net/http package. Callers should not compare errors against this variable. |
net/http | ProtocolError | Not all errors in the http package related to protocol errors are of type ProtocolError. |
net/http/httputil | ErrClosed | No longer used. |
net/http/httputil | ErrPersistEOF | No longer used. |
net/http/httputil | ErrPipeline | No longer used. |
path/filepath | HasPrefix | HasPrefix does not respect path boundaries and does not ignore case when required. |
修補版本
| 版本 | 發布日期 | 含安全修補 | 內容(官方原文) |
|---|---|---|---|
go1.8.1 | 2017-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.2 | 2017-05-23 | 是 | includes a security fix to the crypto/elliptic package. |
go1.8.3 | 2017-05-24 | 否 | includes fixes to the compiler, runtime, documentation, and the database/sql package. |
go1.8.4 | 2017-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.5 | 2017-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.6 | 2018-01-22 | 否 | includes the same fix in math/big as Go 1.9.3 and was released at the same time. |
go1.8.7 | 2018-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-0201 | CVE-2018-6574 | Remote command execution via "go get" command with cgo in cmd/go | go1.8.7、go1.9.4 |
| GO-2022-0187 | CVE-2017-8932 | Incorrect computation for P-256 curves in crypto/elliptic | go1.7.6、go1.8.2 |
| GO-2022-0177 | CVE-2017-15041 | Remote command execution via "go get" in cmd/go | go1.8.4、go1.9.1 |
| GO-2021-0178 | CVE-2017-15042 | Cleartext transmission of credentials in net/smtp | go1.8.4、go1.9.1 |
本版已停止支援,不會再修正這些漏洞;完整清單見 資安漏洞。