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 印出的是原型別名稱。範例:
- package main
- import "fmt"
- type Set[T comparable] map[T]struct{}
- type StringSet = Set[string]
- type Pairs[V any] = map[string]V
- func main() {
- s := StringSet{"go": {}}
- s["rust"] = struct{}{}
- fmt.Println(len(s))
- var p Pairs[int] = map[string]int{"a": 1}
- fmt.Printf("%T %v\n", p, p)
- }
輸出:
- 2
- map[string]int map[a:1]
os.Root
處理使用者上傳的檔名時,如果檔名是
../../etc/passwd,直接 filepath.Join 就可能讀到目錄外的檔案。os.OpenRoot 開啟一個目錄當作根,之後透過 root.Open、root.Create 等方法存取的路徑都不能跑出這個目錄,試圖跳出時回傳錯誤。範例:
- package main
- import (
- "fmt"
- "os"
- "path/filepath"
- )
- func main() {
- dir, err := os.MkdirTemp("", "root-demo")
- if err != nil {
- fmt.Println("error:", err)
- return
- }
- defer os.RemoveAll(dir)
- if err := os.WriteFile(filepath.Join(dir, "note.txt"), []byte("hello"), 0o644); err != nil {
- fmt.Println("error:", err)
- return
- }
- root, err := os.OpenRoot(dir)
- if err != nil {
- fmt.Println("error:", err)
- return
- }
- defer root.Close()
- f, err := root.Open("note.txt")
- if err != nil {
- fmt.Println("error:", err)
- return
- }
- buf := make([]byte, 5)
- n, _ := f.Read(buf)
- f.Close()
- fmt.Println(string(buf[:n]))
- _, err = root.Open("../outside.txt")
- fmt.Println(err != nil)
- }
輸出:
- hello
- true
JSON 的 omitzero
omitempty 只對空字串、0、nil、空切片等有效,遇到 time.Time 這種結構永遠不會省略。omitzero 以「是否為零值」判斷,零值的 time.Time 也會被省略;型別有 IsZero() bool 方法時會用它判斷。詳見 JSON。範例:
- package main
- import (
- "encoding/json"
- "fmt"
- "time"
- )
- type Event struct {
- Name string `json:"name"`
- Retries int `json:"retries,omitzero"`
- At time.Time `json:"at,omitzero"`
- Tags []string `json:"tags,omitempty"`
- }
- func main() {
- out, err := json.Marshal(Event{Name: "deploy"})
- if err != nil {
- fmt.Println("error:", err)
- return
- }
- fmt.Println(string(out))
- out, _ = json.Marshal(Event{Name: "retry", Retries: 2})
- fmt.Println(string(out))
- }
輸出:
- {"name":"deploy"}
- {"name":"retry","retries":2}
字串迭代器函式
strings.Split 會一次建立整個切片,資料很大時浪費記憶體。SplitSeq 回傳迭代器,邊走訪邊切;Lines 逐行走訪,每行保留結尾的 \n;FieldsSeq 依空白切出欄位並忽略多餘空白。範例:
- package main
- import (
- "fmt"
- "strings"
- )
- func main() {
- for part := range strings.SplitSeq("a,b,c", ",") {
- fmt.Print("[", part, "]")
- }
- fmt.Println()
- for line := range strings.Lines("first\nsecond\n") {
- fmt.Printf("%q\n", line)
- }
- for f := range strings.FieldsSeq(" go is fun ") {
- fmt.Print(f, "|")
- }
- fmt.Println()
- }
輸出:
- [a][b][c]
- "first\n"
- "second\n"
- go|is|fun|
本版新增的棄用 API
以下 API 從 Go 1.24 起標記為棄用(Deprecated),新程式不要再使用;說明欄為官方原文。
| 套件 | 符號 | 官方說明 |
|---|---|---|
crypto/cipher | NewCFBDecrypter | CFB 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/cipher | NewCFBEncrypter | CFB 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/cipher | NewOFB | OFB 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. |
runtime | GOROOT | The 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.1 | 2025-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.2 | 2025-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.3 | 2025-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.4 | 2025-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.5 | 2025-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.6 | 2025-08-06 | 是 | includes security fixes to the database/sql and os/exec packages, as well as bug fixes to the runtime. |
go1.24.7 | 2025-09-03 | 否 | includes fixes to the go command, and the net and os/exec packages. |
go1.24.8 | 2025-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.9 | 2025-10-13 | 否 | includes fixes to the crypto/x509 package. |
go1.24.10 | 2025-11-05 | 否 | includes fixes to the encoding/pem and net/url packages. |
go1.24.11 | 2025-12-02 | 是 | includes two security fixes to the crypto/x509 package, as well as bug fixes to the runtime. |
go1.24.12 | 2026-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.13 | 2026-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-4433 | CVE-2025-61732 | Potential code smuggling via doc comments in cmd/cgo | go1.24.13、go1.25.7 |
| GO-2026-4403 | CVE-2025-22873 | Improper access to parent directory of root in os | go1.23.9、go1.24.3 |
| GO-2026-4342 | CVE-2025-61728 | Excessive CPU consumption when building archive index in archive/zip | go1.24.12、go1.25.6 |
| GO-2026-4341 | CVE-2025-61726 | Memory exhaustion in query parameter parsing in net/url | go1.24.12、go1.25.6 |
| GO-2026-4340 | CVE-2025-61730 | Handshake messages may be processed at the incorrect encryption level in crypto/tls | go1.24.12、go1.25.6 |
| GO-2026-4339 | CVE-2025-61731 | Arbitrary file write using cgo pkg-config directive in cmd/go | go1.24.12、go1.25.6 |
| GO-2026-4338 | CVE-2025-68119 | Unexpected code execution when invoking toolchain in cmd/go | go1.24.13、go1.25.6 |
| GO-2026-4337 | CVE-2025-68121 | Unexpected session resumption in crypto/tls | go1.24.13、go1.25.7、go1.26.0-rc.3 |
| GO-2025-4175 | CVE-2025-61727 | Improper application of excluded DNS name constraints when verifying wildcard names in crypto/x509 | go1.24.11、go1.25.5 |
| GO-2025-4155 | CVE-2025-61729 | Excessive resource consumption when printing error string for host certificate validation in crypto/x509 | go1.24.11、go1.25.5 |
| GO-2025-4015 | CVE-2025-61724 | Excessive CPU consumption in Reader.ReadResponse in net/textproto | go1.24.8、go1.25.2 |
| GO-2025-4014 | CVE-2025-58183 | Unbounded allocation when parsing GNU sparse map in archive/tar | go1.24.8、go1.25.2 |
| GO-2025-4013 | CVE-2025-58188 | Panic when validating certificates with DSA public keys in crypto/x509 | go1.24.8、go1.25.2 |
| GO-2025-4012 | CVE-2025-58186 | Lack of limit when parsing cookies can cause memory exhaustion in net/http | go1.24.8、go1.25.2 |
| GO-2025-4011 | CVE-2025-58185 | Parsing DER payload can cause memory exhaustion in encoding/asn1 | go1.24.8、go1.25.2 |
| GO-2025-4010 | CVE-2025-47912 | Insufficient validation of bracketed IPv6 hostnames in net/url | go1.24.8、go1.25.2 |
| GO-2025-4009 | CVE-2025-61723 | Quadratic complexity when parsing some invalid inputs in encoding/pem | go1.24.8、go1.25.2 |
| GO-2025-4008 | CVE-2025-58189 | ALPN negotiation error contains attacker controlled information in crypto/tls | go1.24.8、go1.25.2 |
| GO-2025-4007 | CVE-2025-58187 | Quadratic complexity when checking name constraints in crypto/x509 | go1.24.9、go1.25.3 |
| GO-2025-4006 | CVE-2025-61725 | Excessive CPU consumption in ParseAddress in net/mail | go1.24.8、go1.25.2 |
| 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-3749 | CVE-2025-22874 | Usage of ExtKeyUsageAny disables policy validation in crypto/x509 | 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-3428 | CVE-2025-22867 | Arbitrary code execution during build on darwin in cmd/go | go1.24.0-rc.3 |
| GO-2025-3421 | CVE-2025-22865 | ParsePKCS1PrivateKey panic with partial keys in crypto/x509 | go1.24.0-rc.2 |
| 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-3383 | CVE-2024-45340 | GOAUTH credential leak in cmd/go | 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 |
本版已停止支援,不會再修正這些漏洞;完整清單見 資安漏洞。