# Go 1.6

Go 1.6 發布於 2016-02-17，目前最新修補版為 `go1.6.4`，**已停止支援**，請升級到 `go1.27.4`。

## 版本介紹

Go 1.6 沒有語言變更。最有感的兩件事：`net/http` 在 HTTPS 下自動支援 HTTP/2，程式不用改一行；vendoring 正式成為預設功能，專案可以把依賴放在 `vendor` 目錄裡一起管理。此外也正式訂定 Go 與 C 之間共用指標的規則，並由執行時檢查。

### 工具

- vendoring 預設啟用，不再是實驗功能（仍可用 `GO15VENDOREXPERIMENT=0` 關閉）。
- cgo 訂定指標傳遞規則：傳給 C 的 Go 記憶體不得再含有指向 Go 記憶體的指標，C 也不能在呼叫結束後保留指標；違反時執行時會直接當掉並說明原因。
- `go vet` 會檢查把函式值本身（而非呼叫結果）傳給 `Printf` 的錯誤；`go build` 新增 `-msan`（LLVM 記憶體檢查工具）支援。
- 新增 Linux 64 位元 MIPS 與 Android 32 位元 x86 平台。

### 執行時與效能

- 執行時加入輕量的映射並行誤用偵測：一邊寫、一邊有其他 goroutine 讀寫同一個映射時，程式會直接終止並印出診斷。
- 程式因 panic 結束時，預設只印出當前 goroutine 的堆疊，輸出更精簡；需要全部時設定 `GOTRACEBACK=all`。
- 垃圾回收暫停時間比 1.5 更低，大量使用記憶體的程式尤其明顯。
- `compress/gzip`、`crypto/aes`、`sort` 等套件效能提升超過 10%。

### 標準庫重點

- `net/http` 在 HTTPS 下自動協商 HTTP/2，用戶端與伺服器端皆然。
- `text/template` 新增 `{{-`、`-}}` 去除動作（action）前後的空白，並新增 `{{block}}` 定義可被覆寫的預設區塊。
- `sort.Sort` 改寫，比較與交換次數減少約 10%；相等元素的最終順序可能和以前不同，需要保留原順序請用 `sort.Stable`。

### 樣板空白修剪與 block

`{{-` 會去掉動作前面的空白與換行，`-}}` 去掉後面的。`{{block "name" .}}` 等於「定義並立刻執行」一個樣板，之後可以用 `{{define}}` 換掉它的內容。

```go
package main

import (
	"os"
	"text/template"
)

const page = `{{define "title"}}預設標題{{end -}}
<h1>{{block "title" .}}{{end}}</h1>
<ul>
{{- range .}}
  <li>{{.}}</li>
{{- end}}
</ul>
`

func main() {
	t := template.Must(template.New("page").Parse(page))
	t.Execute(os.Stdout, []string{"apple", "banana"})

	custom := template.Must(template.Must(t.Clone()).Parse(`{{define "title"}}水果清單{{end}}`))
	custom.Execute(os.Stdout, []string{"cherry"})
}
```

```text
<h1>預設標題</h1>
<ul>
  <li>apple</li>
  <li>banana</li>
</ul>
<h1>水果清單</h1>
<ul>
  <li>cherry</li>
</ul>
```

## 本版新增的棄用 API

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

| 套件 | 符號 | 官方說明 |
| --- | --- | --- |
| `compress/flate` | `ReadError` | No longer returned. |
| `compress/flate` | `WriteError` | No longer returned. |
| `net/http` | `Transport.CancelRequest` | Use [Request.WithContext] to create a request with a cancelable context instead. CancelRequest cannot cancel HTTP/2 requests. This may become a no-op in a future release of Go. |

## 修補版本

| 版本 | 發布日期 | 含安全修補 | 內容（官方原文） |
| --- | --- | --- | --- |
| `go1.6.1` | 2016-04-12 | 是 | includes two security fixes. |
| `go1.6.2` | 2016-04-20 | 否 | includes fixes to the compiler, runtime, tools, documentation, and the mime/multipart, net/http, and sort packages. |
| `go1.6.3` | 2016-07-17 | 是 | includes security fixes to the net/http/cgi package and net/http package when used in a CGI environment. |
| `go1.6.4` | 2016-12-01 | 是 | includes two security fixes. It contains the same fixes as Go 1.7.4 and was released at the same time. |

## 已知漏洞

在 Go 1.6 系列修補版中修正的漏洞共 5 個；仍影響最新修補版 `go1.6.4` 的漏洞共 208 個。

### 本系列已修正

| 編號 | CVE | 摘要（官方原文） | 修正於 |
| --- | --- | --- | --- |
| [GO-2022-0761](https://pkg.go.dev/vuln/GO-2022-0761) | CVE-2016-5386 | Improper input validation in net/http and net/http/cgi | go1.6.3 |
| [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-2022-0166](https://pkg.go.dev/vuln/GO-2022-0166) | CVE-2016-3959 | Denial of service due to unchecked parameters in crypto/dsa | go1.5.4、go1.6.1 |
| [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 |
| [GO-2021-0163](https://pkg.go.dev/vuln/GO-2021-0163) | CVE-2016-3958 | Privilege escalation on Windows via malicious DLL in syscall | go1.5.4、go1.6.1 |

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


## 資料來源

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