Go 1.25
最後更新
Go 1.25 發布於 2025-08-12,目前最新修補版為
go1.25.14,已停止支援,請升級到 go1.27.4。版本介紹
Go 1.25 沒有語言變更,重點放在執行時與工具:在容器中
GOMAXPROCS 會自動依 CPU 限制設定,testing/synctest 正式推出讓並行程式測試不再依賴真實時間,sync.WaitGroup 新增 Go 方法,net/http 內建跨來源請求偽造(CSRF)防護。新的 Green Tea GC 與 encoding/json/v2 也以實驗形式登場。升級時要注意:編譯器修正了一個自 Go 1.21 起延遲 nil 指標檢查的錯誤,「先用回傳值、後檢查 err」的錯誤寫法現在會正確地 panic;Go 1.25 需要 macOS 12 以上。語言變更
- 沒有影響程式的語言變更;語言規格移除了「核心型別(core type)」的概念,改用更直接的文字描述。
工具
go.mod新增ignore指令,讓./...等樣式略過指定目錄。go doc -http啟動本機文件伺服器並在瀏覽器開啟。go version -m -json以 JSON 印出執行檔內嵌的建置資訊。- 新增
work套件樣式,代表工作區內所有模組的套件。 - 發行包附帶的預編譯工具變少,不常用的工具改由
go tool需要時再編譯。 go vet新增waitgroup(Add位置錯誤)與hostport(用Sprintf組host:port不支援 IPv6)檢查。
執行時與效能
- Linux 上
GOMAXPROCS預設會參考 cgroup 的 CPU 頻寬限制(例如 Kubernetes 的 CPU limit),並在限制變動時定期更新。 - 實驗性的 Green Tea GC(
GOEXPERIMENT=greenteagc)改善小物件的標記與掃描,GC 開銷預期降低 10–40%,於 Go 1.26 成為預設,詳見 GC。 - 新增
runtime/trace.FlightRecorder,在記憶體中持續錄製執行追蹤,事件發生時才把最近幾秒寫出。 - 編譯器能在更多情況把切片底層陣列配置在堆疊上;除錯資訊改用 DWARF 5,執行檔更小、連結更快。
- 重新 panic 的訊息改為
panic: X [recovered, repanicked],不再重複印出值。
標準庫重點
testing/synctest正式推出:在隔離的「泡泡」中執行測試並使用虛擬時鐘,time.Sleep會瞬間完成,詳見 synctest。sync.WaitGroup.Go一次完成Add(1)、啟動 goroutine 與Done(),詳見 WaitGroup。net/http新增CrossOriginProtection,依Sec-Fetch-Site等標頭拒絕跨來源的不安全請求。reflect.TypeAssert[T]直接把reflect.Value轉回具體型別,效果如同對Value.Interface()做型別斷言,但避免不必要的記憶體配置。hash新增Cloner介面,標準庫所有雜湊都能複製目前狀態。- 實驗性的
encoding/json/v2與encoding/json/jsontext(GOEXPERIMENT=jsonv2),於 Go 1.27 正式推出。
sync.WaitGroup.Go
以前啟動一組 goroutine 要寫
wg.Add(1)、go func(){ defer wg.Done() ... }() 三步,忘了任何一步就會卡住或提前結束。wg.Go(f) 把這三步合成一個呼叫,Wait 仍用來等待全部完成。範例:
- package main
- import (
- "fmt"
- "sync"
- )
- func main() {
- var wg sync.WaitGroup
- results := make([]int, 4)
- for i := range 4 {
- wg.Go(func() {
- results[i] = i * i
- })
- }
- wg.Wait()
- fmt.Println(results)
- }
輸出:
- [0 1 4 9]
CrossOriginProtection
CSRF 是指惡意網站誘導使用者的瀏覽器,帶著登入狀態對你的網站送出請求。
http.NewCrossOriginProtection() 建立防護,Handler 包住原本的處理器:瀏覽器標示為跨站(cross-site)的 POST 等不安全請求會被回 403 Forbidden,同源請求與 GET 不受影響。範例用 httptest.NewRecorder 直接呼叫處理器。範例:
- package main
- import (
- "fmt"
- "net/http"
- "net/http/httptest"
- )
- func main() {
- mux := http.NewServeMux()
- mux.HandleFunc("POST /transfer", func(w http.ResponseWriter, r *http.Request) {
- fmt.Fprint(w, "ok")
- })
- handler := http.NewCrossOriginProtection().Handler(mux)
- send := func(site string) {
- req := httptest.NewRequest("POST", "https://bank.example/transfer", nil)
- if site != "" {
- req.Header.Set("Sec-Fetch-Site", site)
- }
- rec := httptest.NewRecorder()
- handler.ServeHTTP(rec, req)
- fmt.Println(site, rec.Code)
- }
- send("same-origin")
- send("cross-site")
- }
輸出:
- same-origin 200
- cross-site 403
reflect.TypeAssert
用反射(reflection)取出值時,以前要寫
v.Interface().(int),先轉成介面再斷言。reflect.TypeAssert[T](v) 一步完成,型別不符時回傳零值與 false,不會 panic。詳見 reflect。範例:
- package main
- import (
- "fmt"
- "reflect"
- )
- func main() {
- v := reflect.ValueOf(42)
- n, ok := reflect.TypeAssert[int](v)
- fmt.Println(n, ok)
- s, ok := reflect.TypeAssert[string](v)
- fmt.Printf("%q %v\n", s, ok)
- }
輸出:
- 42 true
- "" false
實驗性的 encoding/json/v2
v2 的預設行為更嚴格:重複的鍵會回傳錯誤,
Deterministic(true) 讓映射依鍵排序輸出。這個套件在 Go 1.25 是實驗功能,需要在建置時設定環境變數才看得到:- GOEXPERIMENT=jsonv2 go run main.go
範例:
- package main
- import (
- "encoding/json/v2"
- "fmt"
- )
- type Config struct {
- Host string `json:"host"`
- Port int `json:"port"`
- }
- func main() {
- var c Config
- err := json.Unmarshal([]byte(`{"host":"localhost","port":8080}`), &c)
- fmt.Println(c, err)
- err = json.Unmarshal([]byte(`{"host":"a","host":"b"}`), &c)
- fmt.Println(err != nil)
- out, _ := json.Marshal(map[string]int{"b": 2, "a": 1}, json.Deterministic(true))
- fmt.Println(string(out))
- }
輸出:
- {localhost 8080} <nil>
- true
- {"a":1,"b":2}
本版新增的棄用 API
以下 API 從 Go 1.25 起標記為棄用(Deprecated),新程式不要再使用;說明欄為官方原文。
| 套件 | 符號 | 官方說明 |
|---|---|---|
go/ast | FilterFuncDuplicates | use the type checker [go/types] instead of [Package]; see [Object]. |
go/ast | FilterImportDuplicates | use the type checker [go/types] instead of [Package]; see [Object]. |
go/ast | FilterPackage | use the type checker [go/types] instead of [Package]; see [Object]. Alternatively, use [FilterFile]. |
go/ast | FilterUnassociatedComments | use the type checker [go/types] instead of [Package]; see [Object]. |
go/ast | MergeMode | use the type checker [go/types] instead of [Package]; see [Object]. |
go/ast | MergePackageFiles | this function is poorly specified and has unfixable bugs; also [Package] is deprecated. |
go/ast | PackageExports | use the type checker [go/types] instead of [Package]; see [Object]. Alternatively, use [FileExports]. |
go/parser | ParseDir | ParseDir does not consider build tags when associating files with packages. For precise information about the relationship between packages and files, use golang.org/x/tools/go/packages, which can also optionally parse and type-check the files too. |
修補版本
| 版本 | 發布日期 | 含安全修補 | 內容(官方原文) |
|---|---|---|---|
go1.25.1 | 2025-09-03 | 是 | includes security fixes to the net/http package, as well as bug fixes to the go command, and the net, os, os/exec, and testing/synctest packages. |
go1.25.2 | 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 runtime, and the context, debug/pe, net/http, os, and sync/atomic packages. |
go1.25.3 | 2025-10-13 | 否 | includes fixes to the crypto/x509 package. |
go1.25.4 | 2025-11-05 | 否 | includes fixes to the compiler, the runtime, and the crypto/subtle, encoding/pem, net/url, and os packages. |
go1.25.5 | 2025-12-02 | 是 | includes two security fixes to the crypto/x509 package, as well as bug fixes to the mime and os packages. |
go1.25.6 | 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, errors, and os packages. |
go1.25.7 | 2026-02-04 | 是 | includes security fixes to the go command and the crypto/tls package, as well as bug fixes to the compiler and the crypto/x509 package. |
go1.25.8 | 2026-03-05 | 是 | includes security fixes to the html/template, net/url, and os packages, as well as bug fixes to the go command, the compiler, and the os package. |
go1.25.9 | 2026-04-07 | 是 | includes security fixes to the go command, the compiler, and the archive/tar, crypto/tls, crypto/x509, html/template, and os packages, as well as bug fixes to the go command, the compiler, and the runtime. |
go1.25.10 | 2026-05-07 | 是 | includes security fixes to the go command, the pack tool, and the html/template, net, net/http, net/http/httputil, net/mail, and syscall packages, as well as bug fixes to the go command, the compiler, the linker, the runtime, and the crypto/fips140, go/types, and os packages. |
go1.25.11 | 2026-06-02 | 是 | includes security fixes to the crypto/x509, mime, and net/textproto packages, as well as bug fixes to the compiler and the runtime. |
go1.25.12 | 2026-07-07 | 是 | includes security fixes to the crypto/tls and os packages, as well as bug fixes to the compiler, the go command, and the net and os packages. |
go1.25.13 | 2026-08-13 | 是 | includes security fixes to the go command, and the crypto/tls, encoding/asn1, encoding/xml, html/template, net/http, and net/url packages, as well as bug fixes to the compiler, the runtime, and the crypto/tls and os packages. |
go1.25.14 | 2026-08-19 | 否 | includes fixes to the net/http package. |
已知漏洞
在 Go 1.25 系列修補版中修正的漏洞共 57 個;仍影響最新修補版
go1.25.14 的漏洞共 33 個。本系列已修正
| 編號 | CVE | 摘要(官方原文) | 修正於 |
|---|---|---|---|
| GO-2026-6218 | CVE-2026-56860 | Avoid quadratic complexity in resolvePath in net/url | go1.25.13、go1.26.6、go1.27.0-rc.3 |
| GO-2026-6180 | CVE-2026-56864 | Ignore unrelated, unauthenticated hashes in Lookup in golang.org/x/mod/sumdb | go1.25.13、go1.26.6、go1.27.0-rc.3 |
| GO-2026-6179 | CVE-2026-56865 | Fix transparency log tile verification bypass in golang.org/x/mod/sumdb/tlog | go1.25.13、go1.26.6、go1.27.0-rc.3 |
| GO-2026-6091 | CVE-2026-56858 | Fix Javascript regexp context tracking in html/template | go1.25.13、go1.26.6、go1.27.0-rc.3 |
| GO-2026-6090 | CVE-2026-56862 | Limit handshake messages we are willing to accept post-handshake in crypto/tls | go1.25.13、go1.26.6、go1.27.0-rc.3 |
| GO-2026-6089 | CVE-2026-56853 | Apply ReadHeaderTimeout when doing unencrypted HTTP/2 check in net/http | go1.25.13、go1.26.6、go1.27.0-rc.3 |
| GO-2026-6088 | CVE-2026-56859 | Add recursion depth guard during decode in encoding/xml | go1.25.13、go1.26.6、go1.27.0-rc.3 |
| GO-2026-5972 | CVE-2026-33818 | Enforce maximum recursion depth in encoding/asn1 | go1.25.13、go1.26.6、go1.27.0-rc.3 |
| GO-2026-5856 | CVE-2026-42505 | Invoking Encrypted Client Hello privacy leak in crypto/tls | go1.25.12、go1.26.5、go1.27.0-rc.2 |
| GO-2026-5039 | CVE-2026-42507 | Arbitrary inputs are included in errors without any escaping in net/textproto | go1.25.11、go1.26.4 |
| GO-2026-5038 | CVE-2026-42504 | Quadratic complexity in WordDecoder.DecodeHeader in mime | go1.25.11、go1.26.4 |
| GO-2026-5037 | CVE-2026-27145 | Inefficient candidate hostname parsing in crypto/x509 | go1.25.11、go1.26.4 |
| GO-2026-5026 | CVE-2026-39821 | Invoking failure to reject ASCII-only Punycode-encoded labels in golang.org/x/net/idna | go1.25.13、go1.26.6、go1.27.0-rc.3 |
| GO-2026-4986 | CVE-2026-39820 | Quadratic string concatentation in consumeComment in net/mail | go1.25.10、go1.26.3 |
| GO-2026-4984 | CVE-2026-42501 | Malicious module proxy can bypass checksum database in cmd/go | go1.25.10、go1.26.3 |
| GO-2026-4982 | CVE-2026-39823 | Bypass of meta content URL escaping causes XSS in html/template | go1.25.10、go1.26.3 |
| GO-2026-4981 | CVE-2026-33811 | Crash when handling long CNAME response in net | go1.25.10、go1.26.3 |
| GO-2026-4980 | CVE-2026-39826 | Escaper bypass leads to XSS in html/template | go1.25.10、go1.26.3 |
| GO-2026-4979 | CVE-2026-39817 | Invoking "go tool pack" does not sanitize output paths in cmd/go | go1.25.10、go1.26.3 |
| GO-2026-4978 | CVE-2026-39819 | Invoking "go bug" follows symlinks in predictable temporary filenames in cmd/go | go1.25.10、go1.26.3 |
| GO-2026-4977 | CVE-2026-42499 | Quadratic string concatenation in consumePhrase in net/mail | go1.25.10、go1.26.3 |
| GO-2026-4976 | CVE-2026-39825 | ReverseProxy forwards queries with more than urlmaxqueryparams parameters in net/http/httputil | go1.25.10、go1.26.3 |
| GO-2026-4971 | CVE-2026-39836 | Panic in Dial and LookupPort when handling NUL byte on Windows in net | go1.25.10、go1.26.3 |
| GO-2026-4970 | CVE-2026-39822 | Root escape via symlink plus trailing slash in os | go1.25.12、go1.26.5、go1.27.0-rc.2 |
| GO-2026-4947 | CVE-2026-32280 | Unexpected work during chain building in crypto/x509 | go1.25.9、go1.26.2 |
| GO-2026-4946 | CVE-2026-32281 | Inefficient policy validation in crypto/x509 | go1.25.9、go1.26.2 |
| GO-2026-4918 | CVE-2026-33814 | Infinite loop in HTTP/2 transport when given bad SETTINGS_MAX_FRAME_SIZE in net/http/internal/http2 in golang.org/x/net | go1.25.10、go1.26.3 |
| GO-2026-4871 | CVE-2026-27140 | Code execution vulnerability in SWIG code generation in cmd/go | go1.25.9、go1.26.2 |
| GO-2026-4870 | CVE-2026-32283 | Unauthenticated TLS 1.3 KeyUpdate record can cause persistent connection retention and DoS in crypto/tls | go1.25.9、go1.26.2 |
| GO-2026-4869 | CVE-2026-32288 | Unbounded allocation for old GNU sparse in archive/tar | go1.25.9、go1.26.2 |
| GO-2026-4868 | CVE-2026-27143 | Missing bound checks can lead to memory corruption in safe Go in cmd/compile | go1.25.9、go1.26.2 |
| GO-2026-4867 | CVE-2026-27144 | Miscompilation allows memory corruption via CONVNOP-wrapped array copy in cmd/compile | go1.25.9、go1.26.2 |
| GO-2026-4865 | CVE-2026-32289 | JsBraceDepth Context Tracking Bugs (XSS) in html/template | go1.25.9、go1.26.2 |
| GO-2026-4864 | CVE-2026-32282 | TOCTOU permits root escape on Linux via Root.Chmod in os in internal/syscall/unix | go1.25.9、go1.26.2 |
| GO-2026-4603 | CVE-2026-27142 | URLs in meta content attribute actions are not escaped in html/template | go1.25.8、go1.26.1 |
| GO-2026-4602 | CVE-2026-27139 | FileInfo can escape from a Root in os | go1.25.8、go1.26.1 |
| GO-2026-4601 | CVE-2026-25679 | Incorrect parsing of IPv6 host literals in net/url | go1.25.8、go1.26.1 |
| GO-2026-4433 | CVE-2025-61732 | Potential code smuggling via doc comments in cmd/cgo | go1.24.13、go1.25.7 |
| 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-3955 | CVE-2025-47910 | CrossOriginProtection insecure bypass patterns not limited to exact matches in net/http | go1.25.1 |
本版已停止支援,不會再修正這些漏洞;完整清單見 資安漏洞。