# Go 1.4

Go 1.4 發布於 2014-12-10，目前最新修補版為 `go1.4.3`，**已停止支援**，請升級到 `go1.27.4`。

## 版本介紹

Go 1.4 的語言變更只有一個小語法：`for range x` 可以不寫任何迴圈變數。真正的重點在底層：大部分執行時（runtime）從 C 改寫成 Go，垃圾回收器因此能完全精確，為下一版的並行垃圾回收鋪路；工具上則加入了 `go generate` 與 `internal` 套件機制。

### 語言變更

- 允許 `for range x { ... }`，只在乎迴圈次數、不需要索引或值時更簡潔。
- 編譯器不再允許對 `**T`（指標的指標）型別的值直接呼叫方法，這是依規格修正的行為。

### 工具

- 新增 `go generate`，在編譯前執行原始碼中 `//go:generate` 註記的指令，例如用 `stringer` 為常數產生 `String` 方法。
- 新增 `internal` 套件：路徑含 `internal` 的套件只能被其上層目錄樹內的程式匯入（此版僅對 Go 主儲存庫強制，Go 1.5 起對所有專案生效）。
- 新增標準匯入路徑（canonical import path）註記，例如 `package pdf // import "rsc.io/pdf"`，避免同一套件被以不同路徑匯入。
- 子儲存庫的匯入路徑改為 `golang.org/x/...`。
- 標準庫原始碼從 `src/pkg/...` 移到 `src/...`。

### 執行時與效能

- 執行時大部分改用 Go 撰寫，垃圾回收器能準確知道所有指標的位置，堆積大小比上一版小 10%–30%。
- 堆疊全面改為連續堆疊，滿了就配置更大的堆疊並整段複製，徹底解決「hot split」效能問題。
- 新增寫入屏障（write barrier），為之後的並行垃圾回收做準備；整體效能與 1.3 相近。

### 標準庫重點

- 此版沒有新增套件。
- `bufio.Scanner` 修正在輸入結尾時對分割函式（split function）的呼叫行為。
- `syscall` 套件凍結，只為維持標準庫運作而修改，新的系統呼叫功能移到 `golang.org/x/sys`。

### 省略變數的 for range

不需要索引也不需要值時，直接寫 `for range`。對通道使用時，會一直接收到通道關閉為止。

```go
package main

import "fmt"

func main() {
	tasks := []string{"build", "test", "deploy"}

	count := 0
	for range tasks {
		count++
	}
	fmt.Println("tasks:", count)

	ch := make(chan int, 3)
	ch <- 1
	ch <- 2
	ch <- 3
	close(ch)
	for range ch {
		fmt.Print("tick ")
	}
	fmt.Println()
}
```

```text
tasks: 3
tick tick tick 
```

## 本版新增的棄用 API

本版沒有新增棄用 API。

## 修補版本

| 版本 | 發布日期 | 含安全修補 | 內容（官方原文） |
| --- | --- | --- | --- |
| `go1.4.1` | 2015-01-15 | 否 | includes bug fixes to the linker and the log, syscall, and runtime packages. |
| `go1.4.2` | 2015-02-17 | 是 | includes security fixes to the compiler, and bug fixes to the go command, the compiler and linker, and the runtime, syscall, reflect, and math/big packages. |
| `go1.4.3` | 2015-09-22 | 是 | includes security fixes to the net/http package and bug fixes to the runtime package. |

## 已知漏洞

在 Go 1.4 系列修補版中修正的漏洞共 1 個；仍影響最新修補版 `go1.4.3` 的漏洞共 212 個。

### 本系列已修正

| 編號 | CVE | 摘要（官方原文） | 修正於 |
| --- | --- | --- | --- |
| [GO-2021-0159](https://pkg.go.dev/vuln/GO-2021-0159) | CVE-2015-5739 | Request smuggling due to improper header parsing in net/http | go1.4.3 |

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


## 資料來源

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