# 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`。

```go
package main

import (
	"context"
	"errors"
	"fmt"
	"time"
)

func fetch(ctx context.Context) error {
	select {
	case <-ctx.Done():
		return ctx.Err()
	case <-time.After(time.Second):
		return nil
	}
}

func main() {
	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
	defer cancel()

	err := fetch(ctx)
	fmt.Println(err)
	fmt.Println(errors.Is(err, context.DeadlineExceeded))

	parent, stop := context.WithCancel(context.Background())
	child, childCancel := context.WithCancel(parent)
	defer childCancel()
	stop()
	<-child.Done()
	fmt.Println(child.Err())
}
```

```text
context deadline exceeded
true
context canceled
```

## 本版新增的棄用 API

以下 API 從 Go 1.7 起標記為棄用（Deprecated），新程式不要再使用；說明欄為官方原文。

| 套件 | 符號 | 官方說明 |
| --- | --- | --- |
| `go/build` | `AllowBinary` | The 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. |
| `net` | `Dialer.Cancel` | Use DialContext instead. |
| `net/http` | `ErrWriteAfterFlush` | ErrWriteAfterFlush is no longer returned by anything in the net/http package. Callers should not compare errors against this variable. |
| `net/http` | `Request.Cancel` | Set 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/http` | `Transport.Dial` | Use 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/httputil` | `ClientConn` | Use Client or Transport in package [net/http] instead. |
| `net/http/httputil` | `NewClientConn` | Use the Client or Transport in package [net/http] instead. |
| `net/http/httputil` | `NewProxyClientConn` | Use the Client or Transport in package [net/http] instead. |
| `net/http/httputil` | `NewServerConn` | Use the Server in package [net/http] instead. |
| `net/http/httputil` | `ServerConn` | Use the Server in package [net/http] instead. |
| `os` | `SEEK_CUR` | Use io.SeekStart, io.SeekCurrent, and io.SeekEnd. |
| `os` | `SEEK_END` | Use io.SeekStart, io.SeekCurrent, and io.SeekEnd. |
| `os` | `SEEK_SET` | Use io.SeekStart, io.SeekCurrent, and io.SeekEnd. |
| `syscall` | `AttachLsf` | Use golang.org/x/net/bpf instead. |
| `syscall` | `BpfBuflen` | Use golang.org/x/net/bpf instead. |
| `syscall` | `BpfDatalink` | Use golang.org/x/net/bpf instead. |
| `syscall` | `BpfHeadercmpl` | Use golang.org/x/net/bpf instead. |
| `syscall` | `BpfInterface` | Use golang.org/x/net/bpf instead. |
| `syscall` | `BpfJump` | Use golang.org/x/net/bpf instead. |
| `syscall` | `BpfStats` | Use golang.org/x/net/bpf instead. |
| `syscall` | `BpfStmt` | Use golang.org/x/net/bpf instead. |
| `syscall` | `BpfTimeout` | Use golang.org/x/net/bpf instead. |
| `syscall` | `CheckBpfVersion` | Use golang.org/x/net/bpf instead. |
| `syscall` | `DetachLsf` | Use golang.org/x/net/bpf instead. |
| `syscall` | `FlushBpf` | Use golang.org/x/net/bpf instead. |
| `syscall` | `InterfaceAddrMessage` | Use golang.org/x/net/route instead. |
| `syscall` | `InterfaceAnnounceMessage` | Use golang.org/x/net/route instead. |
| `syscall` | `InterfaceMessage` | Use golang.org/x/net/route instead. |
| `syscall` | `InterfaceMulticastAddrMessage` | Use golang.org/x/net/route instead. |
| `syscall` | `LsfJump` | Use golang.org/x/net/bpf instead. |
| `syscall` | `LsfSocket` | Use golang.org/x/net/bpf instead. |
| `syscall` | `LsfStmt` | Use golang.org/x/net/bpf instead. |
| `syscall` | `ParseRoutingMessage` | Use golang.org/x/net/route instead. |
| `syscall` | `ParseRoutingSockaddr` | Use golang.org/x/net/route instead. |
| `syscall` | `RouteMessage` | Use golang.org/x/net/route instead. |
| `syscall` | `RouteRIB` | Use golang.org/x/net/route instead. |
| `syscall` | `RoutingMessage` | Use golang.org/x/net/route instead. |
| `syscall` | `SetBpf` | Use golang.org/x/net/bpf instead. |
| `syscall` | `SetBpfBuflen` | Use golang.org/x/net/bpf instead. |
| `syscall` | `SetBpfDatalink` | Use golang.org/x/net/bpf instead. |
| `syscall` | `SetBpfHeadercmpl` | Use golang.org/x/net/bpf instead. |
| `syscall` | `SetBpfImmediate` | Use golang.org/x/net/bpf instead. |
| `syscall` | `SetBpfInterface` | Use golang.org/x/net/bpf instead. |
| `syscall` | `SetBpfPromisc` | Use golang.org/x/net/bpf instead. |
| `syscall` | `SetBpfTimeout` | Use golang.org/x/net/bpf instead. |
| `syscall` | `SetLsfPromisc` | Use golang.org/x/net/bpf instead. |

## 修補版本

| 版本 | 發布日期 | 含安全修補 | 內容（官方原文） |
| --- | --- | --- | --- |
| `go1.7.1` | 2016-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.3` | 2016-10-19 | 否 | includes fixes to the compiler, runtime, and the crypto/cipher, crypto/tls, net/http, and strings packages. |
| `go1.7.4` | 2016-12-01 | 是 | includes two security fixes. |
| `go1.7.5` | 2017-01-26 | 否 | includes fixes to the compiler, runtime, and the crypto/x509 and time packages. |
| `go1.7.6` | 2017-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-0187](https://pkg.go.dev/vuln/GO-2022-0187) | CVE-2017-8932 | Incorrect computation for P-256 curves in crypto/elliptic | go1.7.6、go1.8.2 |
| [GO-2022-0171](https://pkg.go.dev/vuln/GO-2022-0171) | CVE-2017-1000097 | Mishandled trust preferences for root certificates on Darwin in crypto/x509 | go1.6.4、go1.7.4 |
| [GO-2021-0172](https://pkg.go.dev/vuln/GO-2021-0172) | CVE-2017-1000098 | Denial of service when parsing large forms in mime/multipart | go1.6.4、go1.7.4 |

本版已停止支援，不會再修正這些漏洞；完整清單見 [資安漏洞](/security)。


## 資料來源

- [Go 1.7 Release Notes](https://go.dev/doc/go1.7)
- [Go 發版歷史](https://go.dev/doc/devel/release#go1.7)
- [Go 漏洞資料庫](https://vuln.go.dev)
- [golang/go 原始碼 go1.7](https://github.com/golang/go/tree/go1.7)
