Go 1.19

最後更新


Go 1.19 發布於 2022-08-02,目前最新修補版為 go1.19.13,已停止支援,請升級到 go1.27.4。

版本介紹


Go 1.19 是一個以打磨為主的版本:語言幾乎沒有變動,重點是讓程式在容器等記憶體有限的環境更好控制。最重要的是 GC 新增軟性記憶體上限(GOMEMLIMIT),以及 sync/atomic 新增型別化的原子操作;Go 的記憶體模型(memory model)也正式修訂,與 C、Java 等語言對齊。升級時要注意:匯入 os 的程式在 Unix 上會自動把開檔上限調到最大,os/exec 也不再從目前目錄找執行檔。

語言變更


  • 只有一項很小的修正:方法宣告中型別參數的作用範圍更正,不影響既有程式。

工具


  • 文件註解(doc comment)支援連結、清單與更清楚的標題語法,gofmt 會自動整理格式。
  • 新增 unix 建置約束(build constraint),//go:build unix 代表所有類 Unix 系統。
  • go test 與 go generate 等指令會把 GOROOT/bin 放在 PATH 最前面,子程序呼叫的 go 與當前工具鏈一致。
  • go vet 的 errorsas 檢查會回報 errors.As 第二個引數傳 *error 的錯誤用法。

執行時與效能


  • 新增軟性記憶體上限,可用 GOMEMLIMIT 環境變數或 runtime/debug.SetMemoryLimit 設定,接近上限時 GC 會更積極回收。
  • 新 goroutine 的初始堆疊大小改依歷史平均使用量配置,減少早期的堆疊擴張與複製。
  • 編譯器用跳躍表(jump table)實作大型整數與字串 switch,在 amd64 與 arm64 上最多快約 20%。
  • 致命錯誤(例如並行寫入映射)印出的堆疊追蹤更精簡。

標準庫重點


  • sync/atomic 新增 Int64、Uint32、Bool、Pointer[T] 等型別,詳見 atomic。
  • fmt 新增 Append、Appendf、Appendln,把格式化結果直接附加到位元組切片。
  • net/url 新增 JoinPath,安全地串接 URL 路徑。
  • os/exec 不再解析相對於目前目錄的執行檔路徑,避免誤執行同目錄下的惡意檔案。
  • sort 改用 pdqsort 演算法,常見情況下排序更快。

型別化的原子操作


以前要寫 atomic.AddInt64(&n, 1),而且必須記得每個存取點都用原子函式。新的 atomic.Int64 等型別把值包起來,只能透過方法存取,不會不小心寫出非原子的讀寫;多個 goroutine 同時累加也不會有資料競爭(data race)。
範例:
  1. package main
  2. import (
  3. "fmt"
  4. "sync"
  5. "sync/atomic"
  6. )
  7. func main() {
  8. var count atomic.Int64
  9. var ready atomic.Bool
  10. var wg sync.WaitGroup
  11. for i := 0; i < 100; i++ {
  12. wg.Add(1)
  13. go func() {
  14. defer wg.Done()
  15. count.Add(1)
  16. }()
  17. }
  18. wg.Wait()
  19. ready.Store(true)
  20. fmt.Println(count.Load(), ready.Load())
  21. }

輸出:
  1. 100 true

軟性記憶體上限


debug.SetMemoryLimit 設定新的上限並回傳舊值;傳入負數代表「只查詢、不修改」。沒有設定 GOMEMLIMIT 時預設是 math.MaxInt64,也就是沒有上限。這個上限是「軟性」的:程式不會因此被終止,而是讓 GC 在接近時更頻繁地回收。詳見 GC 調校。
範例:
  1. package main
  2. import (
  3. "fmt"
  4. "runtime/debug"
  5. )
  6. func main() {
  7. old := debug.SetMemoryLimit(512 << 20)
  8. fmt.Println("previous:", old)
  9. fmt.Println("current:", debug.SetMemoryLimit(-1))
  10. }

輸出:
  1. previous: 9223372036854775807
  2. current: 536870912

注意:如果執行時設了 GOMEMLIMIT 環境變數,第一行印出的會是該設定值。

fmt.Append


fmt.Appendf 的用法與 fmt.Sprintf 相同,但結果附加到既有的 []byte 後面,不會額外產生字串,適合組裝日誌或網路封包這類大量重複的輸出。
範例:
  1. package main
  2. import "fmt"
  3. func main() {
  4. buf := []byte("log: ")
  5. buf = fmt.Appendf(buf, "user=%s id=%d", "gopher", 42)
  6. buf = fmt.Append(buf, " ok")
  7. fmt.Println(string(buf))
  8. }

輸出:
  1. log: user=gopher id=42 ok

url.JoinPath


手動用 + 串接 URL 很容易多出或少掉 /。url.JoinPath 會處理斜線並清理路徑,基底 URL 無法解析時回傳錯誤。
範例:
  1. package main
  2. import (
  3. "fmt"
  4. "net/url"
  5. )
  6. func main() {
  7. u, err := url.JoinPath("https://example.com/api/", "/v1", "users", "42")
  8. if err != nil {
  9. fmt.Println("error:", err)
  10. return
  11. }
  12. fmt.Println(u)
  13. }

輸出:
  1. https://example.com/api/v1/users/42

本版新增的棄用 API


以下 API 從 Go 1.19 起標記為棄用(Deprecated),新程式不要再使用;說明欄為官方原文。
套件符號官方說明
crypto/x509Certificate.CheckCRLSignatureUse [RevocationList.CheckSignatureFrom] instead.
crypto/x509Certificate.CreateCRLthis method does not generate an RFC 5280 conformant X.509 v2 CRL. To generate a standards compliant CRL, use [CreateRevocationList] instead.
crypto/x509ParseCRLUse [ParseRevocationList] instead.
crypto/x509ParseDERCRLUse [ParseRevocationList] instead.
crypto/x509/pkixCertificateListx509.RevocationList should be used instead.
crypto/x509/pkixTBSCertificateListx509.RevocationList should be used instead.
go/docSynopsisNew programs should use [Package.Synopsis] instead, which handles links in text properly.
go/docToHTMLToHTML cannot identify documentation links in the doc comment, because they depend on knowing what package the text came from, which is not included in this API.
go/docToTextToText cannot identify documentation links in the doc comment, because they depend on knowing what package the text came from, which is not included in this API.
io/ioutil(package)As of Go 1.16, the same functionality is now provided by package [io] or package [os], and those implementations should be preferred in new code. See the specific function documentation for details.
io/ioutilDiscardAs of Go 1.16, this value is simply [io.Discard].
io/ioutilNopCloserAs of Go 1.16, this function simply calls [io.NopCloser].
io/ioutilReadAllAs of Go 1.16, this function simply calls [io.ReadAll].
io/ioutilReadDirAs of Go 1.16, [os.ReadDir] is a more efficient and correct choice: it returns a list of [fs.DirEntry] instead of [fs.FileInfo], and it returns partial results in the case of an error midway through reading a directory.
io/ioutilReadFileAs of Go 1.16, this function simply calls [os.ReadFile].
io/ioutilTempDirAs of Go 1.17, this function simply calls [os.MkdirTemp].
io/ioutilTempFileAs of Go 1.17, this function simply calls [os.CreateTemp].
io/ioutilWriteFileAs of Go 1.16, this function simply calls [os.WriteFile].

修補版本


版本發布日期含安全修補內容(官方原文)
go1.19.12022-09-06是includes security fixes to the net/http and net/url packages, as well as bug fixes to the compiler, the go command, the pprof command, the linker, the runtime, and the crypto/tls and crypto/x509 packages.
go1.19.22022-10-04是includes security fixes to the archive/tar, net/http/httputil, and regexp packages, as well as bug fixes to the compiler, the linker, the runtime, and the go/types package.
go1.19.32022-11-01是includes security fixes to the os/exec and syscall packages, as well as bug fixes to the compiler and the runtime.
go1.19.42022-12-06是includes security fixes to the net/http and os packages, as well as bug fixes to the compiler, the runtime, and the crypto/x509, os/exec, and sync/atomic packages.
go1.19.52023-01-10否includes fixes to the compiler, the linker, and the crypto/x509, net/http, sync/atomic, and syscall packages.
go1.19.62023-02-14是includes security fixes to the crypto/tls, mime/multipart, net/http, and path/filepath packages, as well as bug fixes to the go command, the linker, the runtime, and the crypto/x509, net/http, and time packages.
go1.19.72023-03-07是includes a security fix to the crypto/elliptic package, as well as bug fixes to the linker, the runtime, and the crypto/x509 and syscall packages.
go1.19.82023-04-04是includes security fixes to the go/parser, html/template, mime/multipart, net/http, and net/textproto packages, as well as bug fixes to the linker, the runtime, and the time package.
go1.19.92023-05-02是includes three security fixes to the html/template package, as well as bug fixes to the compiler, the runtime, and the crypto/tls and syscall packages.
go1.19.102023-06-06是includes four security fixes to the cmd/go and runtime packages, as well as bug fixes to the compiler, the go command, and the runtime.
go1.19.112023-07-11是includes a security fix to the net/http package, as well as bug fixes to cgo, the cover tool, the go command, the runtime, and the go/printer package.
go1.19.122023-08-01是includes a security fix to the crypto/tls package, as well as bug fixes to the assembler and the compiler.
go1.19.132023-09-06否includes fixes to the go command, and the crypto/tls and net/http packages.

已知漏洞


在 Go 1.19 系列修補版中修正的漏洞共 26 個;仍影響最新修補版 go1.19.13 的漏洞共 124 個。

本系列已修正


編號CVE摘要(官方原文)修正於
GO-2023-1987CVE-2023-29409Large RSA keys can cause high CPU usage in crypto/tlsgo1.19.12、go1.20.7、go1.21.0-rc.4
GO-2023-1878CVE-2023-29406Insufficient sanitization of Host header in net/httpgo1.19.11、go1.20.6
GO-2023-1842CVE-2023-29405Improper sanitization of LDFLAGS with embedded spaces in go command with cgo in cmd/gogo1.19.10、go1.20.5
GO-2023-1841CVE-2023-29404Improper handling of non-optional LDFLAGS in go command with cgo in cmd/gogo1.19.10、go1.20.5
GO-2023-1840CVE-2023-29403Unsafe behavior in setuid/setgid binaries in runtimego1.19.10、go1.20.5
GO-2023-1839CVE-2023-29402Code injection via go command with cgo in cmd/gogo1.19.10、go1.20.5
GO-2023-1753CVE-2023-29400Improper handling of empty HTML attributes in html/templatego1.19.9、go1.20.4
GO-2023-1752CVE-2023-24540Improper handling of JavaScript whitespace in html/templatego1.19.9、go1.20.4
GO-2023-1751CVE-2023-24539Improper sanitization of CSS values in html/templatego1.19.9、go1.20.4
GO-2023-1705CVE-2023-24536Excessive resource consumption in net/http, net/textproto and mime/multipartgo1.19.8、go1.20.3
GO-2023-1704CVE-2023-24534Excessive memory allocation in net/http and net/textprotogo1.19.8、go1.20.3
GO-2023-1703CVE-2023-24538Backticks not treated as string delimiters in html/templatego1.19.8、go1.20.3
GO-2023-1702CVE-2023-24537Infinite loop in parsing in go/scannergo1.19.8、go1.20.3
GO-2023-1621CVE-2023-24532Incorrect calculation on P256 curves in crypto/internal/nistecgo1.19.7、go1.20.2
GO-2023-1571CVE-2022-41723Denial of service via crafted HTTP/2 stream in net/http and golang.org/x/netgo1.19.6、go1.20.1
GO-2023-1570CVE-2022-41724Panic on large handshake records in crypto/tlsgo1.19.6、go1.20.1
GO-2023-1569CVE-2022-41725Excessive resource consumption in mime/multipartgo1.19.6、go1.20.1
GO-2023-1568CVE-2022-41722Path traversal on Windows in path/filepathgo1.19.6、go1.20.1
GO-2022-1144CVE-2022-41717Excessive memory growth in net/http and golang.org/x/net/http2go1.18.9、go1.19.4
GO-2022-1143CVE-2022-41720Restricted file access on Windows in os and net/httpgo1.18.9、go1.19.4
GO-2022-1095CVE-2022-41716Unsanitized NUL in environment variables on Windows in syscall and os/execgo1.18.8、go1.19.3
GO-2022-1039CVE-2022-41715Memory exhaustion when compiling regular expressions in regexp/syntaxgo1.18.7、go1.19.2
GO-2022-1038CVE-2022-2880Incorrect sanitization of forwarded query parameters in net/http/httputilgo1.18.7、go1.19.2
GO-2022-1037CVE-2022-2879Unbounded memory consumption when reading headers in archive/targo1.18.7、go1.19.2
GO-2022-0988CVE-2022-32190Failure to strip relative path components in net/urlgo1.19.1
GO-2022-0969CVE-2022-27664Denial of service in net/http and golang.org/x/net/http2go1.18.6、go1.19.1

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

資料來源