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 仍用來等待全部完成。
範例:
  1. package main
  2. import (
  3. "fmt"
  4. "sync"
  5. )
  6. func main() {
  7. var wg sync.WaitGroup
  8. results := make([]int, 4)
  9. for i := range 4 {
  10. wg.Go(func() {
  11. results[i] = i * i
  12. })
  13. }
  14. wg.Wait()
  15. fmt.Println(results)
  16. }

輸出:
  1. [0 1 4 9]

CrossOriginProtection


CSRF 是指惡意網站誘導使用者的瀏覽器,帶著登入狀態對你的網站送出請求。http.NewCrossOriginProtection() 建立防護,Handler 包住原本的處理器:瀏覽器標示為跨站(cross-site)的 POST 等不安全請求會被回 403 Forbidden,同源請求與 GET 不受影響。範例用 httptest.NewRecorder 直接呼叫處理器。
範例:
  1. package main
  2. import (
  3. "fmt"
  4. "net/http"
  5. "net/http/httptest"
  6. )
  7. func main() {
  8. mux := http.NewServeMux()
  9. mux.HandleFunc("POST /transfer", func(w http.ResponseWriter, r *http.Request) {
  10. fmt.Fprint(w, "ok")
  11. })
  12. handler := http.NewCrossOriginProtection().Handler(mux)
  13. send := func(site string) {
  14. req := httptest.NewRequest("POST", "https://bank.example/transfer", nil)
  15. if site != "" {
  16. req.Header.Set("Sec-Fetch-Site", site)
  17. }
  18. rec := httptest.NewRecorder()
  19. handler.ServeHTTP(rec, req)
  20. fmt.Println(site, rec.Code)
  21. }
  22. send("same-origin")
  23. send("cross-site")
  24. }

輸出:
  1. same-origin 200
  2. cross-site 403

reflect.TypeAssert


用反射(reflection)取出值時,以前要寫 v.Interface().(int),先轉成介面再斷言。reflect.TypeAssert[T](v) 一步完成,型別不符時回傳零值與 false,不會 panic。詳見 reflect。
範例:
  1. package main
  2. import (
  3. "fmt"
  4. "reflect"
  5. )
  6. func main() {
  7. v := reflect.ValueOf(42)
  8. n, ok := reflect.TypeAssert[int](v)
  9. fmt.Println(n, ok)
  10. s, ok := reflect.TypeAssert[string](v)
  11. fmt.Printf("%q %v\n", s, ok)
  12. }

輸出:
  1. 42 true
  2. "" false

實驗性的 encoding/json/v2


v2 的預設行為更嚴格:重複的鍵會回傳錯誤,Deterministic(true) 讓映射依鍵排序輸出。這個套件在 Go 1.25 是實驗功能,需要在建置時設定環境變數才看得到:
  1. GOEXPERIMENT=jsonv2 go run main.go

範例:
  1. package main
  2. import (
  3. "encoding/json/v2"
  4. "fmt"
  5. )
  6. type Config struct {
  7. Host string `json:"host"`
  8. Port int `json:"port"`
  9. }
  10. func main() {
  11. var c Config
  12. err := json.Unmarshal([]byte(`{"host":"localhost","port":8080}`), &c)
  13. fmt.Println(c, err)
  14. err = json.Unmarshal([]byte(`{"host":"a","host":"b"}`), &c)
  15. fmt.Println(err != nil)
  16. out, _ := json.Marshal(map[string]int{"b": 2, "a": 1}, json.Deterministic(true))
  17. fmt.Println(string(out))
  18. }

輸出:
  1. {localhost 8080} <nil>
  2. true
  3. {"a":1,"b":2}

本版新增的棄用 API


以下 API 從 Go 1.25 起標記為棄用(Deprecated),新程式不要再使用;說明欄為官方原文。
套件符號官方說明
go/astFilterFuncDuplicatesuse the type checker [go/types] instead of [Package]; see [Object].
go/astFilterImportDuplicatesuse the type checker [go/types] instead of [Package]; see [Object].
go/astFilterPackageuse the type checker [go/types] instead of [Package]; see [Object]. Alternatively, use [FilterFile].
go/astFilterUnassociatedCommentsuse the type checker [go/types] instead of [Package]; see [Object].
go/astMergeModeuse the type checker [go/types] instead of [Package]; see [Object].
go/astMergePackageFilesthis function is poorly specified and has unfixable bugs; also [Package] is deprecated.
go/astPackageExportsuse the type checker [go/types] instead of [Package]; see [Object]. Alternatively, use [FileExports].
go/parserParseDirParseDir 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.12025-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.22025-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.32025-10-13否includes fixes to the crypto/x509 package.
go1.25.42025-11-05否includes fixes to the compiler, the runtime, and the crypto/subtle, encoding/pem, net/url, and os packages.
go1.25.52025-12-02是includes two security fixes to the crypto/x509 package, as well as bug fixes to the mime and os packages.
go1.25.62026-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.72026-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.82026-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.92026-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.102026-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.112026-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.122026-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.132026-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.142026-08-19否includes fixes to the net/http package.

已知漏洞


在 Go 1.25 系列修補版中修正的漏洞共 57 個;仍影響最新修補版 go1.25.14 的漏洞共 33 個。

本系列已修正


編號CVE摘要(官方原文)修正於
GO-2026-6218CVE-2026-56860Avoid quadratic complexity in resolvePath in net/urlgo1.25.13、go1.26.6、go1.27.0-rc.3
GO-2026-6180CVE-2026-56864Ignore unrelated, unauthenticated hashes in Lookup in golang.org/x/mod/sumdbgo1.25.13、go1.26.6、go1.27.0-rc.3
GO-2026-6179CVE-2026-56865Fix transparency log tile verification bypass in golang.org/x/mod/sumdb/tloggo1.25.13、go1.26.6、go1.27.0-rc.3
GO-2026-6091CVE-2026-56858Fix Javascript regexp context tracking in html/templatego1.25.13、go1.26.6、go1.27.0-rc.3
GO-2026-6090CVE-2026-56862Limit handshake messages we are willing to accept post-handshake in crypto/tlsgo1.25.13、go1.26.6、go1.27.0-rc.3
GO-2026-6089CVE-2026-56853Apply ReadHeaderTimeout when doing unencrypted HTTP/2 check in net/httpgo1.25.13、go1.26.6、go1.27.0-rc.3
GO-2026-6088CVE-2026-56859Add recursion depth guard during decode in encoding/xmlgo1.25.13、go1.26.6、go1.27.0-rc.3
GO-2026-5972CVE-2026-33818Enforce maximum recursion depth in encoding/asn1go1.25.13、go1.26.6、go1.27.0-rc.3
GO-2026-5856CVE-2026-42505Invoking Encrypted Client Hello privacy leak in crypto/tlsgo1.25.12、go1.26.5、go1.27.0-rc.2
GO-2026-5039CVE-2026-42507Arbitrary inputs are included in errors without any escaping in net/textprotogo1.25.11、go1.26.4
GO-2026-5038CVE-2026-42504Quadratic complexity in WordDecoder.DecodeHeader in mimego1.25.11、go1.26.4
GO-2026-5037CVE-2026-27145Inefficient candidate hostname parsing in crypto/x509go1.25.11、go1.26.4
GO-2026-5026CVE-2026-39821Invoking failure to reject ASCII-only Punycode-encoded labels in golang.org/x/net/idnago1.25.13、go1.26.6、go1.27.0-rc.3
GO-2026-4986CVE-2026-39820Quadratic string concatentation in consumeComment in net/mailgo1.25.10、go1.26.3
GO-2026-4984CVE-2026-42501Malicious module proxy can bypass checksum database in cmd/gogo1.25.10、go1.26.3
GO-2026-4982CVE-2026-39823Bypass of meta content URL escaping causes XSS in html/templatego1.25.10、go1.26.3
GO-2026-4981CVE-2026-33811Crash when handling long CNAME response in netgo1.25.10、go1.26.3
GO-2026-4980CVE-2026-39826Escaper bypass leads to XSS in html/templatego1.25.10、go1.26.3
GO-2026-4979CVE-2026-39817Invoking "go tool pack" does not sanitize output paths in cmd/gogo1.25.10、go1.26.3
GO-2026-4978CVE-2026-39819Invoking "go bug" follows symlinks in predictable temporary filenames in cmd/gogo1.25.10、go1.26.3
GO-2026-4977CVE-2026-42499Quadratic string concatenation in consumePhrase in net/mailgo1.25.10、go1.26.3
GO-2026-4976CVE-2026-39825ReverseProxy forwards queries with more than urlmaxqueryparams parameters in net/http/httputilgo1.25.10、go1.26.3
GO-2026-4971CVE-2026-39836Panic in Dial and LookupPort when handling NUL byte on Windows in netgo1.25.10、go1.26.3
GO-2026-4970CVE-2026-39822Root escape via symlink plus trailing slash in osgo1.25.12、go1.26.5、go1.27.0-rc.2
GO-2026-4947CVE-2026-32280Unexpected work during chain building in crypto/x509go1.25.9、go1.26.2
GO-2026-4946CVE-2026-32281Inefficient policy validation in crypto/x509go1.25.9、go1.26.2
GO-2026-4918CVE-2026-33814Infinite loop in HTTP/2 transport when given bad SETTINGS_MAX_FRAME_SIZE in net/http/internal/http2 in golang.org/x/netgo1.25.10、go1.26.3
GO-2026-4871CVE-2026-27140Code execution vulnerability in SWIG code generation in cmd/gogo1.25.9、go1.26.2
GO-2026-4870CVE-2026-32283Unauthenticated TLS 1.3 KeyUpdate record can cause persistent connection retention and DoS in crypto/tlsgo1.25.9、go1.26.2
GO-2026-4869CVE-2026-32288Unbounded allocation for old GNU sparse in archive/targo1.25.9、go1.26.2
GO-2026-4868CVE-2026-27143Missing bound checks can lead to memory corruption in safe Go in cmd/compilego1.25.9、go1.26.2
GO-2026-4867CVE-2026-27144Miscompilation allows memory corruption via CONVNOP-wrapped array copy in cmd/compilego1.25.9、go1.26.2
GO-2026-4865CVE-2026-32289JsBraceDepth Context Tracking Bugs (XSS) in html/templatego1.25.9、go1.26.2
GO-2026-4864CVE-2026-32282TOCTOU permits root escape on Linux via Root.Chmod in os in internal/syscall/unixgo1.25.9、go1.26.2
GO-2026-4603CVE-2026-27142URLs in meta content attribute actions are not escaped in html/templatego1.25.8、go1.26.1
GO-2026-4602CVE-2026-27139FileInfo can escape from a Root in osgo1.25.8、go1.26.1
GO-2026-4601CVE-2026-25679Incorrect parsing of IPv6 host literals in net/urlgo1.25.8、go1.26.1
GO-2026-4433CVE-2025-61732Potential code smuggling via doc comments in cmd/cgogo1.24.13、go1.25.7
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-3955CVE-2025-47910CrossOriginProtection insecure bypass patterns not limited to exact matches in net/httpgo1.25.1

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

資料來源