# Go 1.13

Go 1.13 發布於 2019-09-03，目前最新修補版為 `go1.13.15`，**已停止支援**，請升級到 `go1.27.4`。

## 版本介紹

Go 1.13 有兩件影響深遠的事：一是錯誤包裝（error wrapping），`fmt.Errorf` 的 `%w` 搭配 `errors.Is`、`errors.As`，讓錯誤可以一層層加上說明又不失去原本的身分；二是 `go` 命令預設透過 Google 營運的模組鏡像（proxy.golang.org）與校驗和資料庫（sum.golang.org）下載並驗證模組。語言上也加入了更現代的數字字面值寫法。

### 語言變更

- 新增二進位字面值 `0b1011`、八進位字面值 `0o660`，以及十六進位浮點數 `0x1p-2`。
- 數字可以用底線分隔，例如 `1_000_000`、`0b_1010_0110`。
- 虛數後綴 `i` 可用在任何整數或浮點數字面值。
- 位移運算的位移量可以是有號整數，不必再轉成 `uint`；位移量為負數時會 panic。
- 使用模組時，`go.mod` 的 `go` 版本需至少為 1.13 才能用這些語法。

### 工具

- `GOPROXY` 預設為 `https://proxy.golang.org,direct`，新增 `GOSUMDB` 驗證模組校驗和、`GOPRIVATE` 標示不公開的模組路徑。
- `GO111MODULE=auto` 時，只要所在目錄或上層有 `go.mod` 就啟用模組模式，即使位於 `GOPATH/src` 內。
- `go env -w` 可以設定使用者層級的預設環境變數。
- `go version` 可以查詢執行檔是用哪個 Go 版本建置；`go build -trimpath` 移除執行檔中的檔案路徑。
- 新的逃逸分析（escape analysis）更精確，更多變數能配置在堆疊上。

### 執行時與效能

- 索引越界的 panic 訊息會帶出索引與長度，例如 `index out of range [3] with length 1`。
- 大多數 `defer` 的效能提升約 30%。
- 堆積縮小後，執行時會更快把記憶體還給作業系統。

### 標準庫重點

- 錯誤包裝：`fmt.Errorf` 新增 `%w`，`errors` 套件新增 `Unwrap`、`Is`、`As`。
- `crypto/tls` 預設啟用 TLS 1.3。
- 新增 `crypto/ed25519`，實作 Ed25519 簽章。
- `os` 新增 `UserConfigDir`；`sync.Pool` 在垃圾回收後會保留部分物件，不再每次全部清空；`sync.Mutex` 與 `Once.Do` 的快速路徑被內嵌，未競爭時更快。

### 新的數字字面值

底線只是給人看的分隔，不影響數值；`0o755` 的十進位是 493。位移量 `shift` 是有號的 `int`，Go 1.13 起可以直接使用。

```go
package main

import "fmt"

func main() {
	population := 23_400_000
	mask := 0b1111_0000
	perm := 0o755
	color := 0xFF_88_00

	fmt.Println(population)
	fmt.Println(mask, perm, color)
	fmt.Println(0x1p-2)

	shift := -1 + 3
	fmt.Println(1 << shift)
}
```

```text
23400000
240 493 16746496
0.25
4
```

### 錯誤包裝

`%w` 把底層錯誤包起來；`errors.Is` 沿著包裝鏈檢查是否為某個錯誤值，`errors.As` 則找出鏈上第一個符合型別的錯誤並取出來。

```go
package main

import (
	"errors"
	"fmt"
	"os"
)

type QueryError struct {
	Query string
}

func (e *QueryError) Error() string {
	return "bad query: " + e.Query
}

func loadConfig(path string) error {
	_, err := os.Open(path)
	if err != nil {
		return fmt.Errorf("load config %q: %w", path, err)
	}
	return nil
}

func search(q string) error {
	return fmt.Errorf("search failed: %w", &QueryError{Query: q})
}

func main() {
	err := loadConfig("/no/such/file.json")
	fmt.Println(errors.Is(err, os.ErrNotExist))
	fmt.Println(errors.Unwrap(err) != nil)

	var qe *QueryError
	if errors.As(search("a=?"), &qe) {
		fmt.Println("query:", qe.Query)
	}
}
```

```text
true
true
query: a=?
```

## 本版新增的棄用 API

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

| 套件 | 符號 | 官方說明 |
| --- | --- | --- |
| `crypto/tls` | `VersionSSL30` | SSLv3 is cryptographically broken, and is no longer supported by this package. See golang.org/issue/32716. |
| `debug/gosym` | `LineTable.LineToPC` | Use Table's LineToPC method instead. |
| `debug/gosym` | `LineTable.PCToLine` | Use Table's PCToLine method instead. |
| `image` | `ZP` | Use a literal [image.Point] instead. |
| `image` | `ZR` | Use a literal [image.Rectangle] instead. |

## 修補版本

| 版本 | 發布日期 | 含安全修補 | 內容（官方原文） |
| --- | --- | --- | --- |
| `go1.13.1` | 2019-09-25 | 是 | includes security fixes to the net/http and net/textproto packages. |
| `go1.13.2` | 2019-10-17 | 是 | includes security fixes to the compiler and the crypto/dsa package. |
| `go1.13.3` | 2019-10-17 | 否 | includes fixes to the go command, the toolchain, the runtime, and the crypto/ecdsa, net, net/http, and syscall packages. |
| `go1.13.4` | 2019-10-31 | 否 | includes fixes to the net/http and syscall packages. It also fixes an issue on macOS 10.15 Catalina where the non-notarized installer and binaries were being rejected by Gatekeeper. |
| `go1.13.5` | 2019-12-04 | 否 | includes fixes to the go command, the runtime, the linker, and the net/http package. |
| `go1.13.6` | 2020-01-09 | 否 | includes fixes to the runtime and the net/http package. |
| `go1.13.7` | 2020-01-28 | 是 | includes two security fixes to the crypto/x509 package. |
| `go1.13.8` | 2020-02-12 | 否 | includes fixes to the runtime, and the crypto/x509 and net/http packages. |
| `go1.13.9` | 2020-03-19 | 否 | includes fixes to the go command, tools, the runtime, the toolchain, and the crypto/cypher package. |
| `go1.13.10` | 2020-04-08 | 否 | includes fixes to the go command, the runtime, and the os/exec and time packages. |
| `go1.13.11` | 2020-05-14 | 否 | includes fixes to the compiler. |
| `go1.13.12` | 2020-06-01 | 否 | includes fixes to the runtime, and the go/types and math/big packages. |
| `go1.13.13` | 2020-07-14 | 是 | includes security fixes to the crypto/x509 and net/http packages. |
| `go1.13.14` | 2020-07-16 | 否 | includes fixes to the compiler, vet, and the database/sql, net/http, and reflect packages. |
| `go1.13.15` | 2020-08-06 | 是 | includes security fixes to the encoding/binary package. |

## 已知漏洞

在 Go 1.13 系列修補版中修正的漏洞共 7 個；仍影響最新修補版 `go1.13.15` 的漏洞共 189 個。

### 本系列已修正

| 編號 | CVE | 摘要（官方原文） | 修正於 |
| --- | --- | --- | --- |
| [GO-2022-0535](https://pkg.go.dev/vuln/GO-2022-0535) | CVE-2020-0601 | Certificate validation bypass on Windows in crypto/x509 | go1.12.16、go1.13.7 |
| [GO-2022-0229](https://pkg.go.dev/vuln/GO-2022-0229) | CVE-2020-7919 | Panic in certificate parsing in crypto/x509 and golang.org/x/crypto/cryptobyte | go1.12.16、go1.13.7 |
| [GO-2022-0213](https://pkg.go.dev/vuln/GO-2022-0213) | CVE-2019-17596 | Panic on invalid DSA public keys in crypto/dsa | go1.12.11、go1.13.2 |
| [GO-2022-0212](https://pkg.go.dev/vuln/GO-2022-0212) | CVE-2019-16276 | Request smuggling due to accepting invalid headers in net/http via net/textproto | go1.12.10、go1.13.1 |
| [GO-2021-0224](https://pkg.go.dev/vuln/GO-2021-0224) | CVE-2020-15586 | Data race and crash in net/http | go1.13.13、go1.14.5 |
| [GO-2021-0223](https://pkg.go.dev/vuln/GO-2021-0223) | CVE-2020-14039 | Certificate verification error on Windows in crypto/x509 | go1.13.13、go1.14.5 |
| [GO-2021-0142](https://pkg.go.dev/vuln/GO-2021-0142) | CVE-2020-16845 | Unbounded read from invalid inputs in encoding/binary | go1.13.15、go1.14.7 |

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


## 資料來源

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