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)。範例:
- package main
- import (
- "fmt"
- "sync"
- "sync/atomic"
- )
- func main() {
- var count atomic.Int64
- var ready atomic.Bool
- var wg sync.WaitGroup
- for i := 0; i < 100; i++ {
- wg.Add(1)
- go func() {
- defer wg.Done()
- count.Add(1)
- }()
- }
- wg.Wait()
- ready.Store(true)
- fmt.Println(count.Load(), ready.Load())
- }
輸出:
- 100 true
軟性記憶體上限
debug.SetMemoryLimit 設定新的上限並回傳舊值;傳入負數代表「只查詢、不修改」。沒有設定 GOMEMLIMIT 時預設是 math.MaxInt64,也就是沒有上限。這個上限是「軟性」的:程式不會因此被終止,而是讓 GC 在接近時更頻繁地回收。詳見 GC 調校。範例:
- package main
- import (
- "fmt"
- "runtime/debug"
- )
- func main() {
- old := debug.SetMemoryLimit(512 << 20)
- fmt.Println("previous:", old)
- fmt.Println("current:", debug.SetMemoryLimit(-1))
- }
輸出:
- previous: 9223372036854775807
- current: 536870912
注意:如果執行時設了
GOMEMLIMIT 環境變數,第一行印出的會是該設定值。fmt.Append
fmt.Appendf 的用法與 fmt.Sprintf 相同,但結果附加到既有的 []byte 後面,不會額外產生字串,適合組裝日誌或網路封包這類大量重複的輸出。範例:
- package main
- import "fmt"
- func main() {
- buf := []byte("log: ")
- buf = fmt.Appendf(buf, "user=%s id=%d", "gopher", 42)
- buf = fmt.Append(buf, " ok")
- fmt.Println(string(buf))
- }
輸出:
- log: user=gopher id=42 ok
url.JoinPath
手動用
+ 串接 URL 很容易多出或少掉 /。url.JoinPath 會處理斜線並清理路徑,基底 URL 無法解析時回傳錯誤。範例:
- package main
- import (
- "fmt"
- "net/url"
- )
- func main() {
- u, err := url.JoinPath("https://example.com/api/", "/v1", "users", "42")
- if err != nil {
- fmt.Println("error:", err)
- return
- }
- fmt.Println(u)
- }
輸出:
- https://example.com/api/v1/users/42
本版新增的棄用 API
以下 API 從 Go 1.19 起標記為棄用(Deprecated),新程式不要再使用;說明欄為官方原文。
| 套件 | 符號 | 官方說明 |
|---|---|---|
crypto/x509 | Certificate.CheckCRLSignature | Use [RevocationList.CheckSignatureFrom] instead. |
crypto/x509 | Certificate.CreateCRL | this method does not generate an RFC 5280 conformant X.509 v2 CRL. To generate a standards compliant CRL, use [CreateRevocationList] instead. |
crypto/x509 | ParseCRL | Use [ParseRevocationList] instead. |
crypto/x509 | ParseDERCRL | Use [ParseRevocationList] instead. |
crypto/x509/pkix | CertificateList | x509.RevocationList should be used instead. |
crypto/x509/pkix | TBSCertificateList | x509.RevocationList should be used instead. |
go/doc | Synopsis | New programs should use [Package.Synopsis] instead, which handles links in text properly. |
go/doc | ToHTML | ToHTML 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/doc | ToText | ToText 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/ioutil | Discard | As of Go 1.16, this value is simply [io.Discard]. |
io/ioutil | NopCloser | As of Go 1.16, this function simply calls [io.NopCloser]. |
io/ioutil | ReadAll | As of Go 1.16, this function simply calls [io.ReadAll]. |
io/ioutil | ReadDir | As 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/ioutil | ReadFile | As of Go 1.16, this function simply calls [os.ReadFile]. |
io/ioutil | TempDir | As of Go 1.17, this function simply calls [os.MkdirTemp]. |
io/ioutil | TempFile | As of Go 1.17, this function simply calls [os.CreateTemp]. |
io/ioutil | WriteFile | As of Go 1.16, this function simply calls [os.WriteFile]. |
修補版本
| 版本 | 發布日期 | 含安全修補 | 內容(官方原文) |
|---|---|---|---|
go1.19.1 | 2022-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.2 | 2022-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.3 | 2022-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.4 | 2022-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.5 | 2023-01-10 | 否 | includes fixes to the compiler, the linker, and the crypto/x509, net/http, sync/atomic, and syscall packages. |
go1.19.6 | 2023-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.7 | 2023-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.8 | 2023-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.9 | 2023-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.10 | 2023-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.11 | 2023-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.12 | 2023-08-01 | 是 | includes a security fix to the crypto/tls package, as well as bug fixes to the assembler and the compiler. |
go1.19.13 | 2023-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-1987 | CVE-2023-29409 | Large RSA keys can cause high CPU usage in crypto/tls | go1.19.12、go1.20.7、go1.21.0-rc.4 |
| GO-2023-1878 | CVE-2023-29406 | Insufficient sanitization of Host header in net/http | go1.19.11、go1.20.6 |
| GO-2023-1842 | CVE-2023-29405 | Improper sanitization of LDFLAGS with embedded spaces in go command with cgo in cmd/go | go1.19.10、go1.20.5 |
| GO-2023-1841 | CVE-2023-29404 | Improper handling of non-optional LDFLAGS in go command with cgo in cmd/go | go1.19.10、go1.20.5 |
| GO-2023-1840 | CVE-2023-29403 | Unsafe behavior in setuid/setgid binaries in runtime | go1.19.10、go1.20.5 |
| GO-2023-1839 | CVE-2023-29402 | Code injection via go command with cgo in cmd/go | go1.19.10、go1.20.5 |
| GO-2023-1753 | CVE-2023-29400 | Improper handling of empty HTML attributes in html/template | go1.19.9、go1.20.4 |
| GO-2023-1752 | CVE-2023-24540 | Improper handling of JavaScript whitespace in html/template | go1.19.9、go1.20.4 |
| GO-2023-1751 | CVE-2023-24539 | Improper sanitization of CSS values in html/template | go1.19.9、go1.20.4 |
| GO-2023-1705 | CVE-2023-24536 | Excessive resource consumption in net/http, net/textproto and mime/multipart | go1.19.8、go1.20.3 |
| GO-2023-1704 | CVE-2023-24534 | Excessive memory allocation in net/http and net/textproto | go1.19.8、go1.20.3 |
| GO-2023-1703 | CVE-2023-24538 | Backticks not treated as string delimiters in html/template | go1.19.8、go1.20.3 |
| GO-2023-1702 | CVE-2023-24537 | Infinite loop in parsing in go/scanner | go1.19.8、go1.20.3 |
| GO-2023-1621 | CVE-2023-24532 | Incorrect calculation on P256 curves in crypto/internal/nistec | go1.19.7、go1.20.2 |
| GO-2023-1571 | CVE-2022-41723 | Denial of service via crafted HTTP/2 stream in net/http and golang.org/x/net | go1.19.6、go1.20.1 |
| GO-2023-1570 | CVE-2022-41724 | Panic on large handshake records in crypto/tls | go1.19.6、go1.20.1 |
| GO-2023-1569 | CVE-2022-41725 | Excessive resource consumption in mime/multipart | go1.19.6、go1.20.1 |
| GO-2023-1568 | CVE-2022-41722 | Path traversal on Windows in path/filepath | go1.19.6、go1.20.1 |
| GO-2022-1144 | CVE-2022-41717 | Excessive memory growth in net/http and golang.org/x/net/http2 | go1.18.9、go1.19.4 |
| GO-2022-1143 | CVE-2022-41720 | Restricted file access on Windows in os and net/http | go1.18.9、go1.19.4 |
| GO-2022-1095 | CVE-2022-41716 | Unsanitized NUL in environment variables on Windows in syscall and os/exec | go1.18.8、go1.19.3 |
| GO-2022-1039 | CVE-2022-41715 | Memory exhaustion when compiling regular expressions in regexp/syntax | go1.18.7、go1.19.2 |
| GO-2022-1038 | CVE-2022-2880 | Incorrect sanitization of forwarded query parameters in net/http/httputil | go1.18.7、go1.19.2 |
| GO-2022-1037 | CVE-2022-2879 | Unbounded memory consumption when reading headers in archive/tar | go1.18.7、go1.19.2 |
| GO-2022-0988 | CVE-2022-32190 | Failure to strip relative path components in net/url | go1.19.1 |
| GO-2022-0969 | CVE-2022-27664 | Denial of service in net/http and golang.org/x/net/http2 | go1.18.6、go1.19.1 |
本版已停止支援,不會再修正這些漏洞;完整清單見 資安漏洞。