Go 1.26

最後更新


Go 1.26 發布於 2026-02-10,目前最新修補版為 go1.26.11,仍在支援期。

版本介紹


Go 1.26 在語言上帶來兩個實用的小改進:內建函式 new 可以直接接收一個運算式當初始值,泛型型別的約束也能引用自己。工具方面,go fix 全面改寫成「現代化工具(modernizer)」,可一鍵把舊寫法升級成新語法與新 API。執行時則把 Green Tea GC 設為預設,GC 開銷預期降低 10–40%,cgo 呼叫也快了約 30%。升級時要注意:多個 crypto 函式的 rand 參數改為一律忽略、固定使用安全亂數;go mod init 預設寫入較舊的 go 版本;net/url.Parse 會拒絕主機部分含多餘冒號的網址;Go 1.26 是最後支援 macOS 12 的版本。

語言變更


  • new 可以接收運算式,例如 new(42) 建立一個值為 42 的 *int,詳見 內建函式。
  • 泛型型別可以在自己的型別參數清單中引用自己,例如 type Adder[A Adder[A]] interface{ ... }。

工具


  • go fix 改建在與 go vet 相同的分析框架上,內含數十個現代化修正器,並支援 //go:fix inline 指令自動遷移 API。
  • go mod init 用 1.N 版工具鏈時會寫入 go 1.(N-1).0,讓新模組相容於仍受支援的舊版本。
  • 移除 cmd/doc 與 go tool doc,請改用行為相同的 go doc。
  • pprof 網頁介面預設改為火焰圖(flame graph)檢視。

執行時與效能


  • Green Tea GC 成為預設,在新款 amd64 CPU 上還會用向量指令掃描小物件;可用 GOEXPERIMENT=nogreenteagc 暫時關閉。
  • cgo 呼叫的基本開銷降低約 30%。
  • 64 位元平台啟動時會隨機化堆積起始位址,提高記憶體攻擊的難度。
  • 實驗性的 goroutine 洩漏剖析(GOEXPERIMENT=goroutineleakprofile),可找出永遠無法被喚醒的 goroutine,於 Go 1.27 正式推出。
  • 編譯器能在更多情況把切片底層陣列配置在堆疊上。

標準庫重點


  • errors.AsType[T] 是泛型版的 errors.As,型別安全且更快,詳見 錯誤包裝。
  • log/slog 新增 NewMultiHandler,一筆日誌同時送到多個 handler。
  • bytes.Buffer.Peek 讀取接下來 n 個位元組但不移動讀取位置。
  • reflect.Type 新增 Fields、Methods、Ins、Outs,以迭代器走訪欄位、方法與參數。
  • testing 新增 T.ArtifactDir 等方法,提供寫出測試產物的目錄;testing/cryptotest.SetGlobalRandom 讓加密亂數在測試中可重現。
  • 新增 crypto/hpke(混合公鑰加密,含後量子方案);實驗性的 simd/archsimd(GOEXPERIMENT=simd)與 runtime/secret(GOEXPERIMENT=runtimesecret)。
  • io.ReadAll 中間配置更少,通常快約兩倍;os/signal.NotifyContext 取消時會附上收到哪個訊號作為原因。

new 接收運算式


JSON 或 Protocol Buffers 常用指標欄位表示「可有可無」,以前要先宣告變數再取位址:age := 30; p.Age = &age。現在 new(30) 直接建立指向 30 的指標,一個運算式就能填好選填欄位。new(T) 傳型別的舊用法不變。
範例:
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. )
  6. type Person struct {
  7. Name string `json:"name"`
  8. Age *int `json:"age,omitempty"`
  9. }
  10. func main() {
  11. p := new(42)
  12. fmt.Println(*p)
  13. out, _ := json.Marshal(Person{Name: "Amy", Age: new(30)})
  14. fmt.Println(string(out))
  15. out, _ = json.Marshal(Person{Name: "Bob"})
  16. fmt.Println(string(out))
  17. }

輸出:
  1. 42
  2. {"name":"Amy","age":30}
  3. {"name":"Bob"}

自我引用的泛型約束


有時希望型別參數「能和同型別的值做運算」,例如 Add 的參數與回傳值都是自己的型別。以前約束不能引用自己,現在可以寫 Adder[A Adder[A]],代表 A 必須有 Add(A) A 方法。詳見 泛型約束。
範例:
  1. package main
  2. import "fmt"
  3. type Adder[A Adder[A]] interface {
  4. Add(A) A
  5. }
  6. type Money int
  7. func (m Money) Add(o Money) Money { return m + o }
  8. func SumAll[A Adder[A]](first A, rest ...A) A {
  9. total := first
  10. for _, v := range rest {
  11. total = total.Add(v)
  12. }
  13. return total
  14. }
  15. func main() {
  16. fmt.Println(SumAll(Money(10), 20, 30))
  17. }

輸出:
  1. 60

errors.AsType


errors.As 需要先宣告一個目標變數再傳它的指標,傳錯型別要到執行期才 panic。errors.AsType[*fs.PathError](err) 直接回傳轉好的值與是否成功,型別錯誤在編譯期就會被抓到。
範例:
  1. package main
  2. import (
  3. "errors"
  4. "fmt"
  5. "io/fs"
  6. "os"
  7. )
  8. func main() {
  9. _, err := os.Open("/definitely/missing.txt")
  10. if pe, ok := errors.AsType[*fs.PathError](err); ok {
  11. fmt.Println(pe.Op, pe.Path)
  12. fmt.Println(errors.Is(pe.Err, fs.ErrNotExist))
  13. }
  14. }

輸出:
  1. open /definitely/missing.txt
  2. true

slog.NewMultiHandler


正式環境常需要「終端機印給人看的文字格式,同時寫一份 JSON 給日誌系統」。NewMultiHandler 把多個 handler 組合成一個,每筆日誌都會送給每個 handler。範例一個寫到標準輸出、一個寫到記憶體緩衝區,再把緩衝區內容印出。
範例:
  1. package main
  2. import (
  3. "bytes"
  4. "fmt"
  5. "log/slog"
  6. "os"
  7. )
  8. func main() {
  9. noTime := func(groups []string, a slog.Attr) slog.Attr {
  10. if a.Key == slog.TimeKey && len(groups) == 0 {
  11. return slog.Attr{}
  12. }
  13. return a
  14. }
  15. var buf bytes.Buffer
  16. logger := slog.New(slog.NewMultiHandler(
  17. slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ReplaceAttr: noTime}),
  18. slog.NewJSONHandler(&buf, &slog.HandlerOptions{ReplaceAttr: noTime}),
  19. ))
  20. logger.Info("started", "port", 8080)
  21. fmt.Print("json: ", buf.String())
  22. }

輸出:
  1. level=INFO msg=started port=8080
  2. json: {"level":"INFO","msg":"started","port":8080}

本版新增的棄用 API


以下 API 從 Go 1.26 起標記為棄用(Deprecated),新程式不要再使用;說明欄為官方原文。
套件符號官方說明
crypto/ecdsaPrivateKey.Dmodifying the raw value can produce invalid keys, and may invalidate internal optimizations; moreover, [big.Int] methods are not suitable for operating on cryptographic values. To encode and decode PrivateKey values, use [PrivateKey.Bytes] and [ParseRawPrivateKey] or [crypto/x509.MarshalPKCS8PrivateKey] and [crypto/x509.ParsePKCS8PrivateKey]. For ECDH, use [crypto/ecdh].
crypto/ecdsaPublicKey.Xmodifying the raw coordinates can produce invalid keys, and may invalidate internal optimizations; moreover, [big.Int] methods are not suitable for operating on cryptographic values. To encode and decode PublicKey values, use [PublicKey.Bytes] and [ParseUncompressedPublicKey] or [crypto/x509.MarshalPKIXPublicKey] and [crypto/x509.ParsePKIXPublicKey]. For ECDH, use [crypto/ecdh]. For lower-level elliptic curve operations, use a third-party module like filippo.io/nistec.
crypto/ecdsaPublicKey.Ymodifying the raw coordinates can produce invalid keys, and may invalidate internal optimizations; moreover, [big.Int] methods are not suitable for operating on cryptographic values. To encode and decode PublicKey values, use [PublicKey.Bytes] and [ParseUncompressedPublicKey] or [crypto/x509.MarshalPKIXPublicKey] and [crypto/x509.ParsePKIXPublicKey]. For ECDH, use [crypto/ecdh]. For lower-level elliptic curve operations, use a third-party module like filippo.io/nistec.
crypto/rsaDecryptPKCS1v15PKCS #1 v1.5 encryption is dangerous and should not be used. Whether this function returns an error or not discloses secret information. If an attacker can cause this function to run repeatedly and learn whether each instance returned an error then they can decrypt and forge signatures as if they had the private key. See [draft-irtf-cfrg-rsa-guidance-05] for more information. Use [EncryptOAEP] and [DecryptOAEP] instead.
crypto/rsaDecryptPKCS1v15SessionKeyPKCS #1 v1.5 encryption is dangerous and should not be used. The protections implemented by this function are limited and fragile, as explained above. See [draft-irtf-cfrg-rsa-guidance-05] for more information. Use [EncryptOAEP] and [DecryptOAEP] instead.
crypto/rsaEncryptPKCS1v15PKCS #1 v1.5 encryption is dangerous and should not be used. See [draft-irtf-cfrg-rsa-guidance-05] for more information. Use [EncryptOAEP] and [DecryptOAEP] instead.
crypto/rsaPKCS1v15DecryptOptionsPKCS #1 v1.5 encryption is dangerous and should not be used. See [draft-irtf-cfrg-rsa-guidance-05] for more information. Use [EncryptOAEP] and [DecryptOAEP] instead.
net/http/httputilReverseProxy.DirectorUse Rewrite instead.

修補版本


版本發布日期含安全修補內容(官方原文)
go1.26.12026-03-05是includes security fixes to the crypto/x509, html/template, net/url, and os packages, as well as bug fixes to the go command, the go fix command, the compiler, and the os and reflect packages.
go1.26.22026-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 go fix command, the compiler, the linker, the runtime, and the net, net/http, and net/url packages.
go1.26.32026-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 go fix command, the compiler, the linker, the runtime, and the crypto/fips140, crypto/tls, go/types, and os packages.
go1.26.42026-06-02是includes security fixes to the crypto/x509, mime, and net/textproto packages, as well as bug fixes to the compiler, the runtime, the go fix command, and the crypto/fips140 package.
go1.26.52026-07-07是includes security fixes to the crypto/tls and os packages, as well as bug fixes to the compiler, the runtime, the go command, and the net, os, and syscall packages.
go1.26.62026-08-13是includes security fixes to the go command, and the crypto/tls, encoding/asn1, encoding/xml, html/template, net, net/http, and net/url packages, as well as bug fixes to the compiler, the linker, the runtime, and the crypto/tls and os packages.
go1.26.72026-08-19否includes fixes to the net/http package.
go1.26.82026-09-01否includes fixes to cgo, the compiler, the runtime, and the debug/elf and os packages.
go1.26.92026-10-08是includes security fixes to the go command, and the crypto/tls, html/template, net/http, net/textproto, and os packages, as well as bug fixes to the compiler, the linker, the runtime, vet, and the crypto/mlkem, net/http, and os packages.
go1.26.102026-10-19是includes security fixes to the crypto/x509 package.
go1.26.112026-11-10是includes security fixes to cgo, the go command, the compiler, the linker, and the net/http package, as well as bug fixes to the assembler, the compiler, the runtime, toolchain bootstrapping, and the crypto/fips140, crypto/hkdf, crypto/rsa, crypto/tls, net, and net/http packages.

已知漏洞


在 Go 1.26 系列修補版中修正的漏洞共 75 個;仍影響最新修補版 go1.26.11 的漏洞共 0 個。

本系列已修正


編號CVE摘要(官方原文)修正於
GO-2026-6859CVE-2026-97054HTTP/1 server crash due to MaxBytesReader header race in net/httpgo1.26.11、go1.27.4
GO-2026-6858CVE-2026-97033Unexpected code execution via --sysroot, -target, -isysroot, -Wl,-syslibroot, and -x cgo flags in cmd/gogo1.26.11、go1.27.4
GO-2026-6857CVE-2026-97034Arbitrary command execution via GCC spec files in cgo linker search directories in cmd/gogo1.26.11、go1.27.4
GO-2026-6856CVE-2026-97035Unexpected code execution and arbitrary file write via Clang -gen-* and GCC -gnat* flags in cgo in cmd/gogo1.26.11、go1.27.4
GO-2026-6855CVE-2026-97036Arbitrary code execution during build on darwin via "-l to_library" in cgo in cmd/gogo1.26.11、go1.27.4
GO-2026-6854CVE-2026-97037Linker script injection via split -Wl, flags and Darwin or symbol linker flags in cgo in cmd/gogo1.26.11、go1.27.4
GO-2026-6853CVE-2026-97038Arbitrary code execution via crafted .syso files and cgo linker input files in cmd/gogo1.26.11、go1.27.4
GO-2026-6852CVE-2026-97039Flag sanitization bypass via "#cgo pkg-config:" directives in cmd/gogo1.26.11、go1.27.4
GO-2026-6851CVE-2026-97040Cgo directive injection via .syso files disguised as Go object files in cmd/go and cmd/linkgo1.26.11、go1.27.4
GO-2026-6850CVE-2026-97041Host linker flag injection via //go:cgo_* directives in cmd/compile and cmd/linkgo1.26.11、go1.27.4
GO-2026-6849CVE-2026-97042Linker flag injection via cgo_import_dynamic and PE DLL names in cmd/cgo, cmd/compile, and cmd/linkgo1.26.11、go1.27.4
GO-2026-6848CVE-2026-97043Unexpected code execution via escaped quotes in //go:cgo_ldflag in cmd/go and cmd/compilego1.26.11、go1.27.4
GO-2026-6809CVE-2026-111943Distrust compromised intermediate certificates in crypto/x509go1.26.10、go1.27.3
GO-2026-6617CVE-2026-97032HTTP/2 server crash due to HPACK encoder race in net/httpgo1.26.9、go1.27.2
GO-2026-6617CVE-2026-97032HTTP/2 server crash due to HPACK encoder race in net/httpgo1.26.9、go1.27.2
GO-2026-6613CVE-2026-94439HTTP/1 server connection desynchronization after 2xx CONNECT response in net/httpgo1.26.9、go1.27.2
GO-2026-6612CVE-2026-78663Double flow control refund on HTTP/2 server streams in net/httpgo1.26.9、go1.27.2
GO-2026-6612CVE-2026-78663Double flow control refund on HTTP/2 server streams in net/httpgo1.26.9、go1.27.2
GO-2026-6611CVE-2026-78669Excessive CPU consumption from repeated initial window changes in net/httpgo1.26.9、go1.27.2
GO-2026-6611CVE-2026-78669Excessive CPU consumption from repeated initial window changes in net/httpgo1.26.9、go1.27.2
GO-2026-6610CVE-2026-78660HTTP/2 transport accepts malformed framing-related headers in net/httpgo1.26.9、go1.27.2
GO-2026-6610CVE-2026-78660HTTP/2 transport accepts malformed framing-related headers in net/httpgo1.26.9、go1.27.2
GO-2026-6609CVE-2026-78667Lack of limit on size of parsed Range headers in net/httpgo1.26.9、go1.27.2
GO-2026-6608CVE-2026-94440Memory limit bypass when parsing MIME headers in net/textproto, mime/multipartgo1.26.9、go1.27.2
GO-2026-6607CVE-2026-97031Reject malformed ECH outer extension references in crypto/tlsgo1.26.9、go1.27.2
GO-2026-6605CVE-2026-56866HTTP/1 client connection desynchronization after CONNECT rejection in net/httpgo1.26.9、go1.27.2
GO-2026-6604CVE-2026-56857Root.Mkdir(All) can follow junctions out of the root on Windows in osgo1.26.9、go1.27.2
GO-2026-6603CVE-2026-78659HTTP/2 server memory exhaustion due to Trailer headers in net/httpgo1.26.9、go1.27.2
GO-2026-6603CVE-2026-78659HTTP/2 server memory exhaustion due to Trailer headers in net/httpgo1.26.9、go1.27.2
GO-2026-6602CVE-2026-94447Checksum database bypass for golang.org/toolchain in cmd/gogo1.26.9、go1.27.2
GO-2026-6601CVE-2026-94444Checksum bypass for golang.org/fips140 in cmd/gogo1.26.9、go1.27.2
GO-2026-6600CVE-2026-97030Recognize yield as regexp preceder keyword in html/templatego1.26.9、go1.27.2
GO-2026-6599CVE-2026-94448Reset context tracking on consecutive template expressions in html/templatego1.26.9、go1.27.2
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-5942CVE-2026-46600Parsing an invalid SVCB or HTTPS RR can panic in golang.org/x/net/dns/dnsmessagego1.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-4866CVE-2026-33810Case-sensitive excludedSubtrees name constraints cause Auth Bypass in crypto/x509go1.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-4600CVE-2026-27138Panic in name constraint checking for malformed certificates in crypto/x509go1.26.1
GO-2026-4599CVE-2026-27137Incorrect enforcement of email constraints in crypto/x509go1.26.1
GO-2026-4337CVE-2025-68121Unexpected session resumption in crypto/tlsgo1.24.13、go1.25.7、go1.26.0-rc.3

資料來源