Go 1.17
最後更新
Go 1.17 發布於 2021-08-16,目前最新修補版為
go1.17.13,已停止支援,請升級到 go1.27.4。版本介紹
Go 1.17 在 64 位元 x86(Linux、macOS、Windows)上改用暫存器傳遞函式參數與回傳值,程式平均快約 5%、執行檔小約 2%,不必改任何程式碼。模組方面引入「模組圖修剪(module graph pruning)」,
go.mod 會列出所有間接依賴,go 命令因此只需讀取真正用到的模組。語言上則新增切片轉陣列指標的轉換。語言變更
- 切片可以轉換成陣列指標:
(*[N]T)(s),結果與切片共用底層陣列;切片長度小於N時會 panic,這是第一個可能在執行時失敗的型別轉換。 - 新增
unsafe.Add(ptr, len)做指標位移,以及unsafe.Slice(ptr, len)從指標建立切片。
工具
go.mod宣告go 1.17以上時啟用模組圖修剪與延遲載入,間接依賴另放在一個require區塊;可用go mod tidy -go=1.17升級。- 模組作者可在
go.mod加上// Deprecated:註記棄用整個模組,go get會顯示警告。 - 建置限制改用
//go:build布林運算式,取代// +build,gofmt會自動同步兩種寫法。 go run example.com/cmd@v1.0.0可以直接執行指定版本的程式,不必先安裝。go vet新增檢查://go:build與// +build不一致、signal.Notify使用無緩衝通道、Is/As/Unwrap方法簽名錯誤。
執行時與效能
- 以暫存器為基礎的呼叫慣例(register-based calling convention),效能提升約 5%。
- 未捕捉的 panic 堆疊追蹤會逐一列出每個參數,比以前的十六進位記憶體內容好讀。
- 含有閉包(closure)的函式也可以被內嵌。
標準庫重點
time新增Time.UnixMilli、Time.UnixMicro、time.UnixMilli、time.UnixMicro,以及Time.IsDST與Layout常數。testing新增T.Setenv(測試期間設定環境變數)與-shuffle旗標(打亂測試執行順序)。net/url與net/http不再接受分號;作為查詢參數分隔符號。reflect新增Value.CanConvert,可先確認轉換是否會成功;sync/atomic的Value新增Swap、CompareAndSwap。runtime/cgo新增Handle,安全地在 Go 與 C 之間傳遞 Go 值。
切片轉陣列指標
(*[2]int)(s) 取得指向切片前兩個元素的陣列指標,透過它修改會反映到原切片。長度不足時會 panic,範例用 recover 接住並印出訊息。- package main
- import "fmt"
- func main() {
- s := []int{1, 2, 3, 4}
- p := (*[2]int)(s)
- p[0] = 100
- fmt.Println(s, *p)
- defer func() {
- fmt.Println("recovered:", recover())
- }()
- _ = (*[8]int)(s)
- }
- [100 2 3 4] [100 2]
- recovered: runtime error: cannot convert slice with length 4 to array or pointer to array with length 8
毫秒與微秒時間戳
過去要取得毫秒時間戳得寫
t.UnixNano() / 1e6,Go 1.17 起直接用 UnixMilli;反向轉換則用 time.UnixMilli,回傳的時間位於本地時區,範例中轉回 UTC 以固定輸出。- package main
- import (
- "fmt"
- "time"
- )
- func main() {
- t := time.Date(2021, 8, 16, 0, 0, 0, 500_000_000, time.UTC)
- fmt.Println(t.UnixMilli())
- fmt.Println(t.UnixMicro())
- back := time.UnixMilli(t.UnixMilli()).UTC()
- fmt.Println(back.Format(time.RFC3339Nano))
- }
- 1629072000500
- 1629072000500000
- 2021-08-16T00:00:00.5Z
本版新增的棄用 API
以下 API 從 Go 1.17 起標記為棄用(Deprecated),新程式不要再使用;說明欄為官方原文。
| 套件 | 符號 | 官方說明 |
|---|---|---|
reflect | Value.InterfaceData | The memory representation of interface values is not compatible with InterfaceData. |
syscall | CreateIoCompletionPort | CreateIoCompletionPort has the wrong function signature. Use x/sys/windows.CreateIoCompletionPort. |
syscall | GetQueuedCompletionStatus | GetQueuedCompletionStatus has the wrong function signature. Use x/sys/windows.GetQueuedCompletionStatus. |
syscall | PostQueuedCompletionStatus | PostQueuedCompletionStatus has the wrong function signature. Use x/sys/windows.PostQueuedCompletionStatus. |
修補版本
| 版本 | 發布日期 | 含安全修補 | 內容(官方原文) |
|---|---|---|---|
go1.17.1 | 2021-09-09 | 是 | includes a security fix to the archive/zip package, as well as bug fixes to the compiler, linker, the go command, and the crypto/rand, embed, go/types, html/template, and net/http packages. |
go1.17.2 | 2021-10-07 | 是 | includes security fixes to linker and the misc/wasm directory, as well as bug fixes to the compiler, runtime, the go command, and the text/template and time packages. |
go1.17.3 | 2021-11-04 | 是 | includes security fixes to the archive/zip and debug/macho packages, as well as bug fixes to the compiler, linker, runtime, the go command, the misc/wasm directory, and the net/http and syscall packages. |
go1.17.4 | 2021-12-02 | 否 | includes fixes to the compiler, linker, runtime, and the go/types, net/http, and time packages. |
go1.17.5 | 2021-12-09 | 是 | includes security fixes to the net/http and syscall packages. |
go1.17.6 | 2022-01-06 | 否 | includes fixes to the compiler, linker, runtime, and the crypto/x509, net/http, and reflect packages. |
go1.17.7 | 2022-02-10 | 是 | includes security fixes to the go command, and the crypto/elliptic and math/big packages, as well as bug fixes to the compiler, linker, runtime, the go command, and the debug/macho, debug/pe, and net/http/httptest packages. |
go1.17.8 | 2022-03-03 | 是 | includes a security fix to the regexp/syntax package, as well as bug fixes to the compiler, runtime, the go command, and the crypto/x509 and net packages. |
go1.17.9 | 2022-04-12 | 是 | includes security fixes to the crypto/elliptic and encoding/pem packages, as well as bug fixes to the linker and runtime. |
go1.17.10 | 2022-05-10 | 是 | includes security fixes to the syscall package, as well as bug fixes to the compiler, runtime, and the crypto/x509 and net/http/httptest packages. |
go1.17.11 | 2022-06-01 | 是 | includes security fixes to the crypto/rand, crypto/tls, os/exec, and path/filepath packages, as well as bug fixes to the crypto/tls package. |
go1.17.12 | 2022-07-12 | 是 | includes security fixes to the compress/gzip, encoding/gob, encoding/xml, go/parser, io/fs, net/http, and path/filepath packages, as well as bug fixes to the compiler, the go command, the runtime, and the runtime/metrics package. |
go1.17.13 | 2022-08-01 | 是 | includes security fixes to the encoding/gob and math/big packages, as well as bug fixes to the compiler and the runtime. |
已知漏洞
在 Go 1.17 系列修補版中修正的漏洞共 27 個;仍影響最新修補版
go1.17.13 的漏洞共 149 個。本系列已修正
| 編號 | CVE | 摘要(官方原文) | 修正於 |
|---|---|---|---|
| GO-2022-0537 | CVE-2022-32189 | Panic when decoding Float and Rat types in math/big | go1.17.13、go1.18.5 |
| GO-2022-0533 | CVE-2022-29804 | Path traversal via Clean on Windows in path/filepath | go1.17.11、go1.18.3 |
| GO-2022-0532 | CVE-2022-30580 | Empty Cmd.Path can trigger unintended binary in os/exec on Windows | go1.17.11、go1.18.3 |
| GO-2022-0531 | CVE-2022-30629 | Session tickets lack random ticket_age_add in crypto/tls | go1.17.11、go1.18.3 |
| GO-2022-0527 | CVE-2022-30630 | Stack exhaustion in Glob on certain paths in io/fs | go1.17.12、go1.18.4 |
| GO-2022-0526 | CVE-2022-30635 | Stack exhaustion when decoding certain messages in encoding/gob | go1.17.12、go1.18.4 |
| GO-2022-0525 | CVE-2022-1705 | Improper sanitization of Transfer-Encoding headers in net/http | go1.17.12、go1.18.4 |
| GO-2022-0524 | CVE-2022-30631 | Stack exhaustion when reading certain archives in compress/gzip | go1.17.12、go1.18.4 |
| GO-2022-0523 | CVE-2022-30633 | Stack exhaustion when unmarshaling certain documents in encoding/xml | go1.17.12、go1.18.4 |
| GO-2022-0522 | CVE-2022-30632 | Stack exhaustion on crafted paths in path/filepath | go1.17.12、go1.18.4 |
| GO-2022-0521 | CVE-2022-28131 | Stack exhaustion from deeply nested XML documents in encoding/xml | go1.17.12、go1.18.4 |
| GO-2022-0520 | CVE-2022-32148 | Exposure of client IP addresses in net/http | go1.17.12、go1.18.4 |
| GO-2022-0515 | CVE-2022-1962 | Stack exhaustion due to deeply nested types in go/parser | go1.17.12、go1.18.4 |
| GO-2022-0493 | CVE-2022-29526 | Incorrect privilege reporting in syscall and golang.org/x/sys/unix | go1.17.10、go1.18.2 |
| GO-2022-0477 | CVE-2022-30634 | Indefinite hang with large buffers on Windows in crypto/rand | go1.17.11、go1.18.3 |
| GO-2022-0435 | CVE-2022-28327 | Panic due to large inputs affecting P-256 curves in crypto/elliptic | go1.17.9、go1.18.1 |
| GO-2022-0433 | CVE-2022-24675 | Stack overflow from a large amount of PEM data in encoding/pem | go1.17.9、go1.18.1 |
| GO-2022-0318 | CVE-2022-23773 | Incorrect access control in the go command in cmd/go/internal/modfetch | go1.16.14、go1.17.7 |
| GO-2022-0289 | CVE-2021-44717 | Misdirected I/O in syscall | go1.16.12、go1.17.5 |
| GO-2022-0288 | CVE-2021-44716 | Unbounded memory growth in net/http and golang.org/x/net/http2 | go1.16.12、go1.17.5 |
| GO-2022-0273 | CVE-2021-39293 | Panic due to crafted inputs in archive/zip | go1.16.8、go1.17.1 |
| GO-2022-0247 | CVE-2021-38297 | Buffer overflow in WASM modules in misc/wasm and cmd/link | go1.16.9、go1.17.2 |
| GO-2021-0347 | CVE-2022-24921 | Stack exhaustion when compiling deeply nested expressions in regexp | go1.16.15、go1.17.8 |
| GO-2021-0319 | CVE-2022-23806 | Incorrect computation for some invalid field elements in crypto/elliptic | go1.16.14、go1.17.7 |
| GO-2021-0317 | CVE-2022-23772 | Uncontrolled memory consumption in math/big | go1.16.14、go1.17.7 |
| GO-2021-0264 | CVE-2021-41772 | Panic when opening certain archives in archive/zip | go1.16.10、go1.17.3 |
| GO-2021-0263 | CVE-2021-41771 | Panic on invalid symbol tables in debug/macho | go1.16.10、go1.17.3 |
本版已停止支援,不會再修正這些漏洞;完整清單見 資安漏洞。