# Go 1.9

Go 1.9 發布於 2017-08-24，目前最新修補版為 `go1.9.7`，**已停止支援**，請升級到 `go1.27.4`。

## 版本介紹

Go 1.9 加入型別別名（type alias），讓大型專案能把型別從一個套件逐步搬到另一個套件而不破壞既有程式。標準庫則帶來好幾個常用工具：`sync.Map` 並行安全映射、`math/bits` 位元運算、測試輔助用的 `t.Helper`，而 `time` 也開始透明地使用單調時鐘（monotonic clock）計算時間差。

### 語言變更

- 新增型別別名 `type T1 = T2`：`T1` 只是 `T2` 的另一個名字，兩者是完全相同的型別。
- 規格明定實作可以把浮點運算融合（例如用 FMA 指令計算 `x*y + z`）；要強制中間結果先捨入，寫成 `float64(x*y) + z`。

### 工具

- 編譯器可以平行編譯同一個套件內的函式，善用多核心。
- `./...` 不再比對 `vendor` 目錄內的套件，要包含時寫 `./vendor/...`。
- Go 安裝目錄搬移後，`go` 工具仍能自動找到正確的 `GOROOT`。
- `go env -json` 以 JSON 輸出；`go test -list` 只列出符合的測試名稱而不執行。

### 執行時與效能

- `runtime.GC`、`debug.SetGCPercent`、`debug.FreeOSMemory` 改為觸發並行垃圾回收，只阻塞呼叫者。
- 大型堆積（超過 50GB）中配置大物件的效能明顯改善；`runtime.ReadMemStats` 在大堆積下也低於 100 微秒。
- 呼叫堆疊可能含有內嵌（inline）的函式框，取得呼叫資訊應使用 `runtime.CallersFrames`。

### 標準庫重點

- `time.Time` 內含單調時鐘讀數，`time.Since`、`Sub` 等計算不受系統時間被調整的影響。
- 新增 `math/bits`，提供計算位元數、前導零、反轉等函式，多數架構上會被編譯成單一 CPU 指令。
- `testing` 新增 `T.Helper`、`B.Helper`，失敗訊息會指向呼叫輔助函式的那一行。
- `sync` 新增 `Map`，適合「寫入少、讀取多」或「各 goroutine 存取不同鍵」的並行情境。
- `runtime/pprof` 支援替效能分析資料加上標籤（label）。

### 型別別名

`Temperature` 只是 `Celsius` 的別名，所以可以直接傳給接受 `Celsius` 的函式，`%T` 印出的也是原本的型別名稱。這與 `type Temperature Celsius`（定義一個全新型別）不同。

```go
package main

import "fmt"

type Celsius float64

type Temperature = Celsius

func describe(c Celsius) string {
	return fmt.Sprintf("%.1f°C", float64(c))
}

func main() {
	var t Temperature = 36.5
	fmt.Println(describe(t))
	fmt.Printf("%T\n", t)

	type Strings = []string
	var s Strings = []string{"a", "b"}
	fmt.Println(len(s))
}
```

```text
36.5°C
main.Celsius
2
```

### math/bits

`0b0010_1100` 有 3 個 1、前面有 2 個 0、後面有 2 個 0；`bits.Len(1000)` 表示 1000 需要 10 個位元。範例中的二進位字面值與底線分隔是 Go 1.13 起的寫法。

```go
package main

import (
	"fmt"
	"math/bits"
)

func main() {
	var x uint8 = 0b0010_1100
	fmt.Println(bits.OnesCount8(x))
	fmt.Println(bits.LeadingZeros8(x))
	fmt.Println(bits.TrailingZeros8(x))
	fmt.Println(bits.Len(1000))
	fmt.Printf("%08b\n", bits.Reverse8(x))
}
```

```text
3
2
2
10
00110100
```

## 本版新增的棄用 API

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

| 套件 | 符號 | 官方說明 |
| --- | --- | --- |
| `database/sql/driver` | `ColumnConverter` | Drivers should implement [NamedValueChecker]. |
| `runtime` | `CPUProfile` | Use the [runtime/pprof] package, or the handlers in the [net/http/pprof] package, or the [testing] package's -test.cpuprofile flag instead. |

## 修補版本

| 版本 | 發布日期 | 含安全修補 | 內容（官方原文） |
| --- | --- | --- | --- |
| `go1.9.1` | 2017-10-04 | 是 | includes two security fixes. |
| `go1.9.2` | 2017-10-25 | 否 | includes fixes to the compiler, linker, runtime, documentation, go command, and the crypto/x509, database/sql, log, and net/smtp packages. It includes a fix to a bug introduced in Go 1.9.1 that broke go get of non-Git repositories under certain conditions. |
| `go1.9.3` | 2018-01-22 | 是 | includes security fixes to the net/url package, as well as bug fixes to the compiler, runtime, and the database/sql, math/big, and net/http packages. |
| `go1.9.4` | 2018-02-07 | 是 | includes a security fix to "go get". |
| `go1.9.5` | 2018-03-28 | 是 | includes security fixes to the go command, as well as bug fixes to the compiler, go command, and the net/http/pprof package. |
| `go1.9.6` | 2018-05-01 | 否 | includes fixes to the compiler and go command. |
| `go1.9.7` | 2018-06-05 | 否 | includes fixes to the go command, and the crypto/x509 and strings packages. In particular, it adds minimal support to the go command for the vgo transition. |

## 已知漏洞

在 Go 1.9 系列修補版中修正的漏洞共 4 個；仍影響最新修補版 `go1.9.7` 的漏洞共 203 個。

### 本系列已修正

| 編號 | CVE | 摘要（官方原文） | 修正於 |
| --- | --- | --- | --- |
| [GO-2022-0203](https://pkg.go.dev/vuln/GO-2022-0203) | CVE-2018-7187 | Remote command execution via "go get" command with "-insecure" option in cmd/go | go1.9.5、go1.10.1 |
| [GO-2022-0201](https://pkg.go.dev/vuln/GO-2022-0201) | CVE-2018-6574 | Remote command execution via "go get" command with cgo in cmd/go | go1.8.7、go1.9.4 |
| [GO-2022-0177](https://pkg.go.dev/vuln/GO-2022-0177) | CVE-2017-15041 | Remote command execution via "go get" in cmd/go | go1.8.4、go1.9.1 |
| [GO-2021-0178](https://pkg.go.dev/vuln/GO-2021-0178) | CVE-2017-15042 | Cleartext transmission of credentials in net/smtp | go1.8.4、go1.9.1 |

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


## 資料來源

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