Go 1.7

最後更新


Go 1.7 發布於 2016-08-15,目前最新修補版為 go1.7.6,已停止支援,請升級到 go1.27.4。

版本介紹


Go 1.7 把 context 套件從 golang.org/x/net 移入標準庫,從此「取消訊號」與「逾時」有了統一的傳遞方式,net/http、os/exec、net 等套件也陸續支援。另一個重點是 x86-64 上的全新 SSA 編譯器後端,程式碼更小、更快,CPU 時間減少 5%–35%。

語言變更


  • 只有一處規格釐清:判斷「終止敘述」時以最後一個非空敘述為準,不影響既有程式。

工具


  • x86-64 啟用基於 SSA(static single assignment)的新編譯器後端,並更積極地消除邊界檢查。
  • 編譯器與連結器速度提升,產生的執行檔通常比 1.6 小,有時達 20%–30%。
  • vendoring 成為標準功能,GO15VENDOREXPERIMENT 環境變數移除。
  • go vet 新增 -lostcancel 檢查,抓出忘記呼叫 context 取消函式的程式。
  • 新增 IBM LinuxOne(s390x)平台;x86-64 程式會保留堆疊框指標(frame pointer),方便 perf 等工具分析。

執行時與效能


  • 執行時觸發的 panic 一律同時實作 error 與 runtime.Error。
  • 新增 runtime.KeepAlive,明確標示物件在某個時間點前仍被使用,常用來延後 finalizer 執行。
  • 垃圾回收與核心函式庫皆有加速,crypto/sha256、strconv、strings 等多個套件效能提升超過 10%。

標準庫重點


  • 新增 context 套件,用 Context 在函式之間傳遞取消訊號、截止時間與請求範圍的值。
  • net/http 的 Request 新增 Context、WithContext 方法。
  • 新增 net/http/httptrace,可追蹤 HTTP 請求中的 DNS 查詢、連線建立等事件。
  • testing 新增子測試(subtest)與子基準測試,用 t.Run、b.Run 撰寫表格驅動測試並共用設定。

context


WithTimeout 讓工作最多等一段時間,時間到 Done() 通道就會關閉、Err() 回傳 context.DeadlineExceeded。取消父 context 時,所有子 context 也會一起被取消。拿到的 cancel 函式一定要呼叫,通常用 defer。
  1. package main
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "time"
  7. )
  8. func fetch(ctx context.Context) error {
  9. select {
  10. case <-ctx.Done():
  11. return ctx.Err()
  12. case <-time.After(time.Second):
  13. return nil
  14. }
  15. }
  16. func main() {
  17. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
  18. defer cancel()
  19. err := fetch(ctx)
  20. fmt.Println(err)
  21. fmt.Println(errors.Is(err, context.DeadlineExceeded))
  22. parent, stop := context.WithCancel(context.Background())
  23. child, childCancel := context.WithCancel(parent)
  24. defer childCancel()
  25. stop()
  26. <-child.Done()
  27. fmt.Println(child.Err())
  28. }

  1. context deadline exceeded
  2. true
  3. context canceled

本版新增的棄用 API


以下 API 從 Go 1.7 起標記為棄用(Deprecated),新程式不要再使用;說明欄為官方原文。
套件符號官方說明
go/buildAllowBinaryThe supported way to create a compiled-only package is to write source code containing a //go:binary-only-package comment at the top of the file. Such a package will be recognized regardless of this flag setting (because it has source code) and will have BinaryOnly set to true in the returned Package.
netDialer.CancelUse DialContext instead.
net/httpErrWriteAfterFlushErrWriteAfterFlush is no longer returned by anything in the net/http package. Callers should not compare errors against this variable.
net/httpRequest.CancelSet the Request's context with NewRequestWithContext instead. If a Request's Cancel field and context are both set, it is undefined whether Cancel is respected.
net/httpTransport.DialUse DialContext instead, which allows the transport to cancel dials as soon as they are no longer needed. If both are set, DialContext takes priority.
net/http/httputilClientConnUse Client or Transport in package [net/http] instead.
net/http/httputilNewClientConnUse the Client or Transport in package [net/http] instead.
net/http/httputilNewProxyClientConnUse the Client or Transport in package [net/http] instead.
net/http/httputilNewServerConnUse the Server in package [net/http] instead.
net/http/httputilServerConnUse the Server in package [net/http] instead.
osSEEK_CURUse io.SeekStart, io.SeekCurrent, and io.SeekEnd.
osSEEK_ENDUse io.SeekStart, io.SeekCurrent, and io.SeekEnd.
osSEEK_SETUse io.SeekStart, io.SeekCurrent, and io.SeekEnd.
syscallAttachLsfUse golang.org/x/net/bpf instead.
syscallBpfBuflenUse golang.org/x/net/bpf instead.
syscallBpfDatalinkUse golang.org/x/net/bpf instead.
syscallBpfHeadercmplUse golang.org/x/net/bpf instead.
syscallBpfInterfaceUse golang.org/x/net/bpf instead.
syscallBpfJumpUse golang.org/x/net/bpf instead.
syscallBpfStatsUse golang.org/x/net/bpf instead.
syscallBpfStmtUse golang.org/x/net/bpf instead.
syscallBpfTimeoutUse golang.org/x/net/bpf instead.
syscallCheckBpfVersionUse golang.org/x/net/bpf instead.
syscallDetachLsfUse golang.org/x/net/bpf instead.
syscallFlushBpfUse golang.org/x/net/bpf instead.
syscallInterfaceAddrMessageUse golang.org/x/net/route instead.
syscallInterfaceAnnounceMessageUse golang.org/x/net/route instead.
syscallInterfaceMessageUse golang.org/x/net/route instead.
syscallInterfaceMulticastAddrMessageUse golang.org/x/net/route instead.
syscallLsfJumpUse golang.org/x/net/bpf instead.
syscallLsfSocketUse golang.org/x/net/bpf instead.
syscallLsfStmtUse golang.org/x/net/bpf instead.
syscallParseRoutingMessageUse golang.org/x/net/route instead.
syscallParseRoutingSockaddrUse golang.org/x/net/route instead.
syscallRouteMessageUse golang.org/x/net/route instead.
syscallRouteRIBUse golang.org/x/net/route instead.
syscallRoutingMessageUse golang.org/x/net/route instead.
syscallSetBpfUse golang.org/x/net/bpf instead.
syscallSetBpfBuflenUse golang.org/x/net/bpf instead.
syscallSetBpfDatalinkUse golang.org/x/net/bpf instead.
syscallSetBpfHeadercmplUse golang.org/x/net/bpf instead.
syscallSetBpfImmediateUse golang.org/x/net/bpf instead.
syscallSetBpfInterfaceUse golang.org/x/net/bpf instead.
syscallSetBpfPromiscUse golang.org/x/net/bpf instead.
syscallSetBpfTimeoutUse golang.org/x/net/bpf instead.
syscallSetLsfPromiscUse golang.org/x/net/bpf instead.

修補版本


版本發布日期含安全修補內容(官方原文)
go1.7.12016-09-07否includes fixes to the compiler, runtime, documentation, and the compress/flate, hash/crc32, io, net, net/http, path/filepath, reflect, and syscall packages.
go1.7.2否go1.7.2 should not be used. It was tagged but not fully released. The release was deferred due to a last minute bug report. Use go1.7.3 instead, and refer to the summary of changes below.
go1.7.32016-10-19否includes fixes to the compiler, runtime, and the crypto/cipher, crypto/tls, net/http, and strings packages.
go1.7.42016-12-01是includes two security fixes.
go1.7.52017-01-26否includes fixes to the compiler, runtime, and the crypto/x509 and time packages.
go1.7.62017-05-23是includes the same security fix as Go 1.8.2 and was released at the same time.

已知漏洞


在 Go 1.7 系列修補版中修正的漏洞共 3 個;仍影響最新修補版 go1.7.6 的漏洞共 207 個。

本系列已修正


編號CVE摘要(官方原文)修正於
GO-2022-0187CVE-2017-8932Incorrect computation for P-256 curves in crypto/ellipticgo1.7.6、go1.8.2
GO-2022-0171CVE-2017-1000097Mishandled trust preferences for root certificates on Darwin in crypto/x509go1.6.4、go1.7.4
GO-2021-0172CVE-2017-1000098Denial of service when parsing large forms in mime/multipartgo1.6.4、go1.7.4

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

資料來源