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) 的型別名稱。
範例:
  1. package main
  2. import (
  3. "fmt"
  4. "iter"
  5. )
  6. func Countdown(n int) iter.Seq[int] {
  7. return func(yield func(int) bool) {
  8. for i := n; i > 0; i-- {
  9. if !yield(i) {
  10. return
  11. }
  12. }
  13. }
  14. }
  15. func main() {
  16. for v := range Countdown(5) {
  17. if v == 2 {
  18. break
  19. }
  20. fmt.Println(v)
  21. }
  22. }

輸出:
  1. 5
  2. 4
  3. 3

slices 與 maps 的迭代器函式


映射的走訪順序不固定,以前要排序鍵得先手動收集。現在 maps.Keys 回傳鍵的迭代器,slices.Sorted 收集後排序,一行完成。slices.Backward 由後往前走訪並同時給索引,slices.Collect 把迭代器收集成切片。
範例:
  1. package main
  2. import (
  3. "fmt"
  4. "maps"
  5. "slices"
  6. )
  7. func main() {
  8. stock := map[string]int{"pear": 3, "apple": 5, "fig": 1}
  9. names := slices.Sorted(maps.Keys(stock))
  10. fmt.Println(names)
  11. for i, v := range slices.Backward([]string{"a", "b", "c"}) {
  12. fmt.Print(i, v, " ")
  13. }
  14. fmt.Println()
  15. upper := slices.Collect(maps.Values(map[string]int{"only": 7}))
  16. fmt.Println(upper)
  17. }

輸出:
  1. [apple fig pear]
  2. 2c 1b 0a
  3. [7]

iter.Pull


range 是「推」模式:迭代器主動把值推給迴圈。有時需要「拉」模式,自己決定何時取下一個值,例如同時走訪兩個序列。iter.Pull 把迭代器轉成 next 與 stop 兩個函式;next 回傳值與是否還有資料,用完一定要呼叫 stop 釋放資源,詳見 iter.Pull。
範例:
  1. package main
  2. import (
  3. "fmt"
  4. "iter"
  5. "slices"
  6. )
  7. func main() {
  8. next, stop := iter.Pull(slices.Values([]string{"a", "b"}))
  9. defer stop()
  10. for {
  11. v, ok := next()
  12. if !ok {
  13. fmt.Println("done")
  14. break
  15. }
  16. fmt.Println(v)
  17. }
  18. }

輸出:
  1. a
  2. b
  3. done

unique


程式裡常有大量內容相同的字串,例如城市代碼、標籤。unique.Make 回傳一個 Handle,內容相同的值會拿到相同的 handle,比較兩個 handle 只要比對指標,比逐字比較字串快;Value() 取回原本的值。
範例:
  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. "unique"
  6. )
  7. func main() {
  8. a := unique.Make("tw-" + strings.ToLower("TAIPEI"))
  9. b := unique.Make("tw-taipei")
  10. c := unique.Make("jp-tokyo")
  11. fmt.Println(a == b, a == c)
  12. fmt.Println(a.Value())
  13. }

輸出:
  1. true false
  2. tw-taipei

本版新增的棄用 API


本版沒有新增棄用 API。

修補版本


版本發布日期含安全修補內容(官方原文)
go1.23.12024-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.22024-10-01否includes fixes to the compiler, cgo, the runtime, and the maps, os, os/exec, time, and unique packages.
go1.23.32024-11-06否includes fixes to the linker, the runtime, and the net/http, os, and syscall packages.
go1.23.42024-12-03否includes fixes to the compiler, the runtime, the trace command, and the syscall package.
go1.23.52025-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.62025-02-04是includes security fixes to the crypto/elliptic package, as well as bug fixes to the compiler and the go command.
go1.23.72025-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.82025-04-01是includes security fixes to the net/http package, as well as bug fixes to the runtime and the go command.
go1.23.92025-05-06否includes fixes to the runtime and the linker.
go1.23.102025-06-05是includes security fixes to the net/http and os packages, as well as bug fixes to the linker.
go1.23.112025-07-08是includes security fixes to the go command, as well as bug fixes to the compiler, the linker, and the runtime.
go1.23.122025-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-4403CVE-2025-22873Improper access to parent directory of root in osgo1.23.9、go1.24.3
GO-2025-3956CVE-2025-47906Unexpected paths returned from LookPath in os/execgo1.23.12、go1.24.6
GO-2025-3849CVE-2025-47907Incorrect results returned from Rows.Scan in database/sqlgo1.23.12、go1.24.6
GO-2025-3828CVE-2025-4674Unexpected command execution in untrusted VCS repositories in cmd/gogo1.23.11、go1.24.5
GO-2025-3751CVE-2025-4673Sensitive headers not cleared on cross-origin redirect in net/httpgo1.23.10、go1.24.4
GO-2025-3750CVE-2025-0913Inconsistent handling of O_CREATE|O_EXCL on Unix and Windows in os in syscallgo1.23.10、go1.24.4
GO-2025-3563CVE-2025-22871Request smuggling due to acceptance of invalid chunked data in net/httpgo1.23.8、go1.24.2
GO-2025-3503CVE-2025-22870HTTP Proxy bypass using IPv6 Zone IDs in golang.org/x/netgo1.23.7、go1.24.1
GO-2025-3447CVE-2025-22866Timing sidechannel for P-256 on ppc64le in crypto/internal/nistecgo1.22.12、go1.23.6、go1.24.0-rc.3
GO-2025-3420CVE-2024-45336Sensitive headers incorrectly sent after cross-domain redirect in net/httpgo1.22.11、go1.23.5、go1.24.0-rc.2
GO-2025-3373CVE-2024-45341Usage of IPv6 zone IDs can bypass URI name constraints in crypto/x509go1.22.11、go1.23.5、go1.24.0-rc.2
GO-2024-3107CVE-2024-34158Stack exhaustion in Parse in go/build/constraintgo1.22.7、go1.23.1
GO-2024-3106CVE-2024-34156Stack exhaustion in Decoder.Decode in encoding/gobgo1.22.7、go1.23.1
GO-2024-3105CVE-2024-34155Stack exhaustion in all Parse functions in go/parsergo1.22.7、go1.23.1

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

資料來源