Go 1.23
最後更新
Go 1.23 發布於 2024-08-13,目前最新修補版為
go1.23.12,已停止支援,請升級到 go1.27.4。版本介紹
Go 1.23 的主角是迭代器(iterator):
for range 可以直接走訪「函式」,讓自訂容器、資料庫查詢結果、檔案行等都能用同一種 for v := range xxx 寫法。標準庫同步加入 iter 套件,slices、maps 也新增一批回傳或接收迭代器的函式。另外新增 unique 套件,並大幅修改 time.Timer 的行為。升級時要注意:go.mod 宣告 go 1.23 以上時,計時器通道改為無緩衝,依賴 len(timer.C) 判斷的程式需要改寫;連結器也開始禁止用 //go:linkname 存取標準庫內部符號。語言變更
for range接受三種函式型別:func(func() bool)、func(func(K) bool)、func(func(K, V) bool),詳見 range over func。- 以預覽方式提供泛型型別別名(
GOEXPERIMENT=aliastypeparams),於 Go 1.24 正式生效。
工具
- 新增 Go 遙測(telemetry):預設只在本機記錄統計,執行
go telemetry on才會匿名上傳。 go mod tidy -diff只印出需要的變更,不修改檔案,適合在 CI 檢查。go env -changed只列出與預設值不同的設定。go.mod與go.work新增godebug指令,為整個模組設定 GODEBUG。go vet新增stdversion檢查,回報使用了比go.mod版本更新的標準庫符號。
執行時與效能
- 啟用 PGO 的建置時間開銷從可能翻倍降到個位數百分比。
- 編譯器能讓函式中不同區段的區域變數共用堆疊位置,減少堆疊用量。
- 未處理的 panic 訊息第二行起會縮排,與堆疊追蹤區分得更清楚。
標準庫重點
- 新增
iter套件,定義Seq[V]、Seq2[K, V]兩種迭代器型別與Pull,詳見 iter。 slices新增All、Values、Backward、Collect、Sorted、Chunk等;maps新增All、Keys、Values、Insert、Collect,詳見 標準庫的迭代器。- 新增
unique套件,把相同的值正規化成同一份,節省記憶體並加快比較。 time.Timer、Ticker不再需要Stop才能被 GC 回收;通道改為無緩衝,Reset、Stop之後不會再收到舊的值。- 新增
structs套件,目前只有HostLayout,標示結構要符合主機平台的記憶體配置。
range over func
迭代器就是一個接收
yield 函式的函式:每產生一個值就呼叫一次 yield。迴圈本體裡寫 break 時,yield 會回傳 false,迭代器應該立刻停止。iter.Seq[int] 是 func(yield func(int) bool) 的型別名稱。範例:
- package main
- import (
- "fmt"
- "iter"
- )
- func Countdown(n int) iter.Seq[int] {
- return func(yield func(int) bool) {
- for i := n; i > 0; i-- {
- if !yield(i) {
- return
- }
- }
- }
- }
- func main() {
- for v := range Countdown(5) {
- if v == 2 {
- break
- }
- fmt.Println(v)
- }
- }
輸出:
- 5
- 4
- 3
slices 與 maps 的迭代器函式
映射的走訪順序不固定,以前要排序鍵得先手動收集。現在
maps.Keys 回傳鍵的迭代器,slices.Sorted 收集後排序,一行完成。slices.Backward 由後往前走訪並同時給索引,slices.Collect 把迭代器收集成切片。範例:
- package main
- import (
- "fmt"
- "maps"
- "slices"
- )
- func main() {
- stock := map[string]int{"pear": 3, "apple": 5, "fig": 1}
- names := slices.Sorted(maps.Keys(stock))
- fmt.Println(names)
- for i, v := range slices.Backward([]string{"a", "b", "c"}) {
- fmt.Print(i, v, " ")
- }
- fmt.Println()
- upper := slices.Collect(maps.Values(map[string]int{"only": 7}))
- fmt.Println(upper)
- }
輸出:
- [apple fig pear]
- 2c 1b 0a
- [7]
iter.Pull
range 是「推」模式:迭代器主動把值推給迴圈。有時需要「拉」模式,自己決定何時取下一個值,例如同時走訪兩個序列。iter.Pull 把迭代器轉成 next 與 stop 兩個函式;next 回傳值與是否還有資料,用完一定要呼叫 stop 釋放資源,詳見 iter.Pull。範例:
- package main
- import (
- "fmt"
- "iter"
- "slices"
- )
- func main() {
- next, stop := iter.Pull(slices.Values([]string{"a", "b"}))
- defer stop()
- for {
- v, ok := next()
- if !ok {
- fmt.Println("done")
- break
- }
- fmt.Println(v)
- }
- }
輸出:
- a
- b
- done
unique
程式裡常有大量內容相同的字串,例如城市代碼、標籤。
unique.Make 回傳一個 Handle,內容相同的值會拿到相同的 handle,比較兩個 handle 只要比對指標,比逐字比較字串快;Value() 取回原本的值。範例:
- package main
- import (
- "fmt"
- "strings"
- "unique"
- )
- func main() {
- a := unique.Make("tw-" + strings.ToLower("TAIPEI"))
- b := unique.Make("tw-taipei")
- c := unique.Make("jp-tokyo")
- fmt.Println(a == b, a == c)
- fmt.Println(a.Value())
- }
輸出:
- true false
- tw-taipei
本版新增的棄用 API
本版沒有新增棄用 API。
修補版本
| 版本 | 發布日期 | 含安全修補 | 內容(官方原文) |
|---|---|---|---|
go1.23.1 | 2024-09-05 | 是 | includes security fixes to the encoding/gob, go/build/constraint, and go/parser packages, as well as bug fixes to the compiler, the go command, the runtime, and the database/sql, go/types, os, runtime/trace, and unique packages. |
go1.23.2 | 2024-10-01 | 否 | includes fixes to the compiler, cgo, the runtime, and the maps, os, os/exec, time, and unique packages. |
go1.23.3 | 2024-11-06 | 否 | includes fixes to the linker, the runtime, and the net/http, os, and syscall packages. |
go1.23.4 | 2024-12-03 | 否 | includes fixes to the compiler, the runtime, the trace command, and the syscall package. |
go1.23.5 | 2025-01-16 | 是 | includes security fixes to the crypto/x509 and net/http packages, as well as bug fixes to the compiler, the runtime, and the net package. |
go1.23.6 | 2025-02-04 | 是 | includes security fixes to the crypto/elliptic package, as well as bug fixes to the compiler and the go command. |
go1.23.7 | 2025-03-04 | 是 | includes security fixes to the net/http package, as well as bug fixes to cgo, the compiler, and the reflect, runtime, and syscall packages. |
go1.23.8 | 2025-04-01 | 是 | includes security fixes to the net/http package, as well as bug fixes to the runtime and the go command. |
go1.23.9 | 2025-05-06 | 否 | includes fixes to the runtime and the linker. |
go1.23.10 | 2025-06-05 | 是 | includes security fixes to the net/http and os packages, as well as bug fixes to the linker. |
go1.23.11 | 2025-07-08 | 是 | includes security fixes to the go command, as well as bug fixes to the compiler, the linker, and the runtime. |
go1.23.12 | 2025-08-06 | 是 | includes security fixes to the database/sql and os/exec packages, as well as bug fixes to the runtime. |
已知漏洞
在 Go 1.23 系列修補版中修正的漏洞共 14 個;仍影響最新修補版
go1.23.12 的漏洞共 89 個。本系列已修正
| 編號 | CVE | 摘要(官方原文) | 修正於 |
|---|---|---|---|
| GO-2026-4403 | CVE-2025-22873 | Improper access to parent directory of root in os | go1.23.9、go1.24.3 |
| GO-2025-3956 | CVE-2025-47906 | Unexpected paths returned from LookPath in os/exec | go1.23.12、go1.24.6 |
| GO-2025-3849 | CVE-2025-47907 | Incorrect results returned from Rows.Scan in database/sql | go1.23.12、go1.24.6 |
| GO-2025-3828 | CVE-2025-4674 | Unexpected command execution in untrusted VCS repositories in cmd/go | go1.23.11、go1.24.5 |
| GO-2025-3751 | CVE-2025-4673 | Sensitive headers not cleared on cross-origin redirect in net/http | go1.23.10、go1.24.4 |
| GO-2025-3750 | CVE-2025-0913 | Inconsistent handling of O_CREATE|O_EXCL on Unix and Windows in os in syscall | go1.23.10、go1.24.4 |
| GO-2025-3563 | CVE-2025-22871 | Request smuggling due to acceptance of invalid chunked data in net/http | go1.23.8、go1.24.2 |
| GO-2025-3503 | CVE-2025-22870 | HTTP Proxy bypass using IPv6 Zone IDs in golang.org/x/net | go1.23.7、go1.24.1 |
| GO-2025-3447 | CVE-2025-22866 | Timing sidechannel for P-256 on ppc64le in crypto/internal/nistec | go1.22.12、go1.23.6、go1.24.0-rc.3 |
| GO-2025-3420 | CVE-2024-45336 | Sensitive headers incorrectly sent after cross-domain redirect in net/http | go1.22.11、go1.23.5、go1.24.0-rc.2 |
| GO-2025-3373 | CVE-2024-45341 | Usage of IPv6 zone IDs can bypass URI name constraints in crypto/x509 | go1.22.11、go1.23.5、go1.24.0-rc.2 |
| GO-2024-3107 | CVE-2024-34158 | Stack exhaustion in Parse in go/build/constraint | go1.22.7、go1.23.1 |
| GO-2024-3106 | CVE-2024-34156 | Stack exhaustion in Decoder.Decode in encoding/gob | go1.22.7、go1.23.1 |
| GO-2024-3105 | CVE-2024-34155 | Stack exhaustion in all Parse functions in go/parser | go1.22.7、go1.23.1 |
本版已停止支援,不會再修正這些漏洞;完整清單見 資安漏洞。