# Go 1.2

Go 1.2 發布於 2013-12-01，目前最新修補版為 `go1.2.2`，**已停止支援**，請升級到 `go1.27.4`。

## 版本介紹

Go 1.2 是發布週期縮短為約半年一版後的第一個版本，改動較小但實用：語言加入「三索引切片（three-index slice）」可以限制切片容量，排程器開始能在函式呼叫時切換 goroutine，`go test` 也首次支援測試覆蓋率（coverage）。

### 語言變更

- 新增三索引切片 `a[low:high:max]`，在切出新切片時同時指定容量（capacity）。
- 規格明定透過 `nil` 指標存取記憶體一定會 panic，避免讀到錯誤位址的不安全行為。

### 工具

- `go test -cover` 可以顯示測試覆蓋率，搭配 `go tool cover` 產生詳細報告。
- 缺少 `package` 子句的檔案改為編譯錯誤。
- `cgo` 可以連結 C++ 程式碼；`godoc` 與 `vet` 的原始碼移到 go.tools 子儲存庫。

### 執行時與效能

- 排程器在進入函式時有機會搶占（preempt），含函式呼叫的長迴圈不再霸佔執行緒。
- goroutine 初始堆疊從 4KB 提高到 8KB，減少堆疊分段切換的效能問題。
- 新增可調整的執行緒數上限（預設 10,000），可用 `runtime/debug.SetMaxThreads` 修改；`SetMaxStack` 則限制單一 goroutine 的堆疊大小。
- `encoding/json` 編碼快了約 30%，Windows 與 BSD 的網路效能也提升約 30%。

### 標準庫重點

- 新增 `encoding` 套件，定義 `TextMarshaler`、`BinaryMarshaler` 等通用編碼介面。
- `fmt` 的格式字串可以用 `%[n]` 指定要取第幾個參數。
- `text/template` 新增 `eq`、`lt` 等比較函式，並支援 `{{else if}}` 寫法。
- 新增 `image/color/palette` 標準調色盤。

### 三索引切片

`array[2:4:5]` 的長度是 2、容量是 5−2=3。容量被限制後，`append` 超出容量就會配置新的底層陣列，不會覆寫原陣列後面的資料。

```go
package main

import "fmt"

func main() {
	array := [10]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

	a := array[2:4]
	fmt.Println(a, len(a), cap(a))

	b := array[2:4:5]
	fmt.Println(b, len(b), cap(b))

	b = append(b, 100, 200)
	fmt.Println(b)
	fmt.Println(array)
}
```

```text
[2 3] 2 8
[2 3] 2 3
[2 3 100 200]
[0 1 2 3 4 5 6 7 8 9]
```

### fmt 的參數索引

`%[n]` 取第 n 個參數（從 1 開始），之後的動詞接著取 n+1 個。常見用途是同一個值印兩次，例如 `%v` 搭配 `%[1]T` 印出型別。

```go
package main

import "fmt"

func main() {
	fmt.Printf("%[3]s %[1]s %s\n", "a", "b", "c")
	fmt.Printf("%d 的型別是 %[1]T\n", 42)
	fmt.Printf("%[2]*[1]d|\n", 7, 5)
}
```

```text
c a b
42 的型別是 int
    7|
```

## 本版新增的棄用 API

本版沒有新增棄用 API。

## 修補版本

| 版本 | 發布日期 | 含安全修補 | 內容（官方原文） |
| --- | --- | --- | --- |
| `go1.2.1` | 2014-03-02 | 否 | includes bug fixes to the runtime, net, and database/sql packages. |
| `go1.2.2` | 2014-05-05 | 是 | includes a security fix that affects the tour binary included in the binary distributions (thanks to Guillaume T). |

## 已知漏洞

在 Go 1.2 系列修補版中修正的漏洞共 0 個；仍影響最新修補版 `go1.2.2` 的漏洞共 214 個。


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


## 資料來源

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