Go 1.24

最後更新


Go 1.24 發布於 2025-02-11,目前最新修補版為 go1.24.13,已停止支援,請升級到 go1.27.4。

版本介紹


Go 1.24 把泛型型別別名轉為正式功能,並在執行時與標準庫做了大量實用改進:內建映射(map)改用 Swiss Table 實作,整體 CPU 開銷平均降低 2–3%;os.Root 讓檔案操作限制在指定目錄內,避免路徑穿越(path traversal)攻擊;encoding/json 新增 omitzero 標籤;go.mod 可以用 tool 指令管理開發工具。升級時要注意:go vet 會開始回報 fmt.Printf(s) 這種非常數格式字串的呼叫(僅限 go 1.24 以上的模組);Go 1.24 是最後支援 macOS 11 的版本,Linux 需要核心 3.2 以上。

語言變更


  • 泛型型別別名正式支援:別名本身也可以帶型別參數,例如 type Pairs[V any] = map[string]V,詳見 泛型型別。

工具


  • go.mod 新增 tool 指令追蹤開發工具,用 go get -tool 加入、go tool <名稱> 執行,不再需要 tools.go 空白匯入的技巧,詳見 模組工具。
  • go build、go install 新增 -json 旗標,以結構化 JSON 輸出建置結果。
  • go build 會依版本控制的 tag 或 commit 把主模組版本寫進執行檔,有未提交變更時加上 +dirty。
  • 新增 GOAUTH 環境變數,彈性設定私有模組的認證方式。
  • go vet 新增 tests 檢查,找出測試、基準測試、範例函式宣告錯誤(例如名稱或簽名寫錯導致不會執行)。

執行時與效能


  • 內建映射改用 Swiss Table、小物件配置更有效率、執行時內部互斥鎖重寫,平均 CPU 開銷降低 2–3%。
  • 新增 runtime.AddCleanup,比 runtime.SetFinalizer 更彈性、更不易出錯,新程式應優先使用。
  • 新增 weak 套件提供弱指標(weak pointer),可用來實作不阻止 GC 回收的快取。

標準庫重點


  • os.OpenRoot 回傳 os.Root,所有操作都限制在該目錄內,../ 或符號連結都無法跳出。
  • testing.B.Loop 讓基準測試寫成 for b.Loop() { ... },比 b.N 寫法更快也更不容易出錯,詳見 基準測試。
  • encoding/json 新增 omitzero 標籤選項:欄位為零值時省略,對 time.Time 等結構也有效。
  • strings、bytes 新增回傳迭代器的 Lines、SplitSeq、FieldsSeq 等函式。
  • 新增 crypto/mlkem(後量子金鑰交換)以及 crypto/hkdf、crypto/pbkdf2、crypto/sha3 套件,詳見 crypto/mlkem。
  • 新增實驗性的 testing/synctest(GOEXPERIMENT=synctest),於 Go 1.25 正式推出。

泛型型別別名


型別別名(alias)是「同一個型別的另一個名字」,用 = 宣告。現在別名可以帶型別參數,也可以替某個實例化後的泛型型別取短名字。下面 Pairs[int] 與 map[string]int 是完全相同的型別,所以 %T 印出的是原型別名稱。
範例:
  1. package main
  2. import "fmt"
  3. type Set[T comparable] map[T]struct{}
  4. type StringSet = Set[string]
  5. type Pairs[V any] = map[string]V
  6. func main() {
  7. s := StringSet{"go": {}}
  8. s["rust"] = struct{}{}
  9. fmt.Println(len(s))
  10. var p Pairs[int] = map[string]int{"a": 1}
  11. fmt.Printf("%T %v\n", p, p)
  12. }

輸出:
  1. 2
  2. map[string]int map[a:1]

os.Root


處理使用者上傳的檔名時,如果檔名是 ../../etc/passwd,直接 filepath.Join 就可能讀到目錄外的檔案。os.OpenRoot 開啟一個目錄當作根,之後透過 root.Open、root.Create 等方法存取的路徑都不能跑出這個目錄,試圖跳出時回傳錯誤。
範例:
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. )
  7. func main() {
  8. dir, err := os.MkdirTemp("", "root-demo")
  9. if err != nil {
  10. fmt.Println("error:", err)
  11. return
  12. }
  13. defer os.RemoveAll(dir)
  14. if err := os.WriteFile(filepath.Join(dir, "note.txt"), []byte("hello"), 0o644); err != nil {
  15. fmt.Println("error:", err)
  16. return
  17. }
  18. root, err := os.OpenRoot(dir)
  19. if err != nil {
  20. fmt.Println("error:", err)
  21. return
  22. }
  23. defer root.Close()
  24. f, err := root.Open("note.txt")
  25. if err != nil {
  26. fmt.Println("error:", err)
  27. return
  28. }
  29. buf := make([]byte, 5)
  30. n, _ := f.Read(buf)
  31. f.Close()
  32. fmt.Println(string(buf[:n]))
  33. _, err = root.Open("../outside.txt")
  34. fmt.Println(err != nil)
  35. }

輸出:
  1. hello
  2. true

JSON 的 omitzero


omitempty 只對空字串、0、nil、空切片等有效,遇到 time.Time 這種結構永遠不會省略。omitzero 以「是否為零值」判斷,零值的 time.Time 也會被省略;型別有 IsZero() bool 方法時會用它判斷。詳見 JSON。
範例:
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "time"
  6. )
  7. type Event struct {
  8. Name string `json:"name"`
  9. Retries int `json:"retries,omitzero"`
  10. At time.Time `json:"at,omitzero"`
  11. Tags []string `json:"tags,omitempty"`
  12. }
  13. func main() {
  14. out, err := json.Marshal(Event{Name: "deploy"})
  15. if err != nil {
  16. fmt.Println("error:", err)
  17. return
  18. }
  19. fmt.Println(string(out))
  20. out, _ = json.Marshal(Event{Name: "retry", Retries: 2})
  21. fmt.Println(string(out))
  22. }

輸出:
  1. {"name":"deploy"}
  2. {"name":"retry","retries":2}

字串迭代器函式


strings.Split 會一次建立整個切片,資料很大時浪費記憶體。SplitSeq 回傳迭代器,邊走訪邊切;Lines 逐行走訪,每行保留結尾的 \n;FieldsSeq 依空白切出欄位並忽略多餘空白。
範例:
  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. func main() {
  7. for part := range strings.SplitSeq("a,b,c", ",") {
  8. fmt.Print("[", part, "]")
  9. }
  10. fmt.Println()
  11. for line := range strings.Lines("first\nsecond\n") {
  12. fmt.Printf("%q\n", line)
  13. }
  14. for f := range strings.FieldsSeq(" go is fun ") {
  15. fmt.Print(f, "|")
  16. }
  17. fmt.Println()
  18. }

輸出:
  1. [a][b][c]
  2. "first\n"
  3. "second\n"
  4. go|is|fun|

本版新增的棄用 API


以下 API 從 Go 1.24 起標記為棄用(Deprecated),新程式不要再使用;說明欄為官方原文。
套件符號官方說明
crypto/cipherNewCFBDecrypterCFB mode is not authenticated, which generally enables active attacks to manipulate and recover the plaintext. It is recommended that applications use [AEAD] modes instead. The standard library implementation of CFB is also unoptimized and not validated as part of the FIPS 140-3 module. If an unauthenticated [Stream] mode is required, use [NewCTR] instead.
crypto/cipherNewCFBEncrypterCFB mode is not authenticated, which generally enables active attacks to manipulate and recover the plaintext. It is recommended that applications use [AEAD] modes instead. The standard library implementation of CFB is also unoptimized and not validated as part of the FIPS 140-3 module. If an unauthenticated [Stream] mode is required, use [NewCTR] instead.
crypto/cipherNewOFBOFB mode is not authenticated, which generally enables active attacks to manipulate and recover the plaintext. It is recommended that applications use [AEAD] modes instead. The standard library implementation of OFB is also unoptimized and not validated as part of the FIPS 140-3 module. If an unauthenticated [Stream] mode is required, use [NewCTR] instead.
runtimeGOROOTThe root used during the Go build will not be meaningful if the binary is copied to another machine. Use the system path to locate the “go” binary, and use “go env GOROOT” to find its GOROOT.

修補版本


版本發布日期含安全修補內容(官方原文)
go1.24.12025-03-04是includes security fixes to the net/http package, as well as bug fixes to cgo, the compiler, the go command, and the reflect, runtime, and syscall packages.
go1.24.22025-04-01是includes security fixes to the net/http package, as well as bug fixes to the compiler, the runtime, the go command, and the crypto/tls, go/types, net/http, and testing packages.
go1.24.32025-05-06是includes security fixes to the os package, as well as bug fixes to the runtime, the compiler, the linker, the go command, and the crypto/tls and os packages.
go1.24.42025-06-05是includes security fixes to the crypto/x509, net/http, and os packages, as well as bug fixes to the linker, the go command, and the hash/maphash and os packages.
go1.24.52025-07-08是includes security fixes to the go command, as well as bug fixes to the compiler, the linker, the runtime, and the go command.
go1.24.62025-08-06是includes security fixes to the database/sql and os/exec packages, as well as bug fixes to the runtime.
go1.24.72025-09-03否includes fixes to the go command, and the net and os/exec packages.
go1.24.82025-10-07是includes security fixes to the archive/tar, crypto/tls, crypto/x509, encoding/asn1, encoding/pem, net/http, net/mail, net/textproto, and net/url packages, as well as bug fixes to the compiler, the linker, and the debug/pe, net/http, os, and sync/atomic packages.
go1.24.92025-10-13否includes fixes to the crypto/x509 package.
go1.24.102025-11-05否includes fixes to the encoding/pem and net/url packages.
go1.24.112025-12-02是includes two security fixes to the crypto/x509 package, as well as bug fixes to the runtime.
go1.24.122026-01-15是includes security fixes to the go command, and the archive/zip, crypto/tls, and net/url packages, as well as bug fixes to the compiler, the runtime, and the crypto/tls and os packages.
go1.24.132026-02-04是includes security fixes to the go command and the crypto/tls package, as well as bug fixes to the crypto/x509 package.

已知漏洞


在 Go 1.24 系列修補版中修正的漏洞共 34 個;仍影響最新修補版 go1.24.13 的漏洞共 70 個。

本系列已修正


編號CVE摘要(官方原文)修正於
GO-2026-4433CVE-2025-61732Potential code smuggling via doc comments in cmd/cgogo1.24.13、go1.25.7
GO-2026-4403CVE-2025-22873Improper access to parent directory of root in osgo1.23.9、go1.24.3
GO-2026-4342CVE-2025-61728Excessive CPU consumption when building archive index in archive/zipgo1.24.12、go1.25.6
GO-2026-4341CVE-2025-61726Memory exhaustion in query parameter parsing in net/urlgo1.24.12、go1.25.6
GO-2026-4340CVE-2025-61730Handshake messages may be processed at the incorrect encryption level in crypto/tlsgo1.24.12、go1.25.6
GO-2026-4339CVE-2025-61731Arbitrary file write using cgo pkg-config directive in cmd/gogo1.24.12、go1.25.6
GO-2026-4338CVE-2025-68119Unexpected code execution when invoking toolchain in cmd/gogo1.24.13、go1.25.6
GO-2026-4337CVE-2025-68121Unexpected session resumption in crypto/tlsgo1.24.13、go1.25.7、go1.26.0-rc.3
GO-2025-4175CVE-2025-61727Improper application of excluded DNS name constraints when verifying wildcard names in crypto/x509go1.24.11、go1.25.5
GO-2025-4155CVE-2025-61729Excessive resource consumption when printing error string for host certificate validation in crypto/x509go1.24.11、go1.25.5
GO-2025-4015CVE-2025-61724Excessive CPU consumption in Reader.ReadResponse in net/textprotogo1.24.8、go1.25.2
GO-2025-4014CVE-2025-58183Unbounded allocation when parsing GNU sparse map in archive/targo1.24.8、go1.25.2
GO-2025-4013CVE-2025-58188Panic when validating certificates with DSA public keys in crypto/x509go1.24.8、go1.25.2
GO-2025-4012CVE-2025-58186Lack of limit when parsing cookies can cause memory exhaustion in net/httpgo1.24.8、go1.25.2
GO-2025-4011CVE-2025-58185Parsing DER payload can cause memory exhaustion in encoding/asn1go1.24.8、go1.25.2
GO-2025-4010CVE-2025-47912Insufficient validation of bracketed IPv6 hostnames in net/urlgo1.24.8、go1.25.2
GO-2025-4009CVE-2025-61723Quadratic complexity when parsing some invalid inputs in encoding/pemgo1.24.8、go1.25.2
GO-2025-4008CVE-2025-58189ALPN negotiation error contains attacker controlled information in crypto/tlsgo1.24.8、go1.25.2
GO-2025-4007CVE-2025-58187Quadratic complexity when checking name constraints in crypto/x509go1.24.9、go1.25.3
GO-2025-4006CVE-2025-61725Excessive CPU consumption in ParseAddress in net/mailgo1.24.8、go1.25.2
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-3749CVE-2025-22874Usage of ExtKeyUsageAny disables policy validation in crypto/x509go1.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-3428CVE-2025-22867Arbitrary code execution during build on darwin in cmd/gogo1.24.0-rc.3
GO-2025-3421CVE-2025-22865ParsePKCS1PrivateKey panic with partial keys in crypto/x509go1.24.0-rc.2
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-3383CVE-2024-45340GOAUTH credential leak in cmd/gogo1.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

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

資料來源