> For the complete documentation index, see [llms.txt](https://denglj.gitbook.io/essential-go/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://denglj.gitbook.io/essential-go/03-variables/blank-identifier.md).

# 空白标识符

为了避免错误，Go编译器不允许存在未使用的变量。

但是，有些场景下，您不需要把一个值存入一个变量。

在这些场景下，您就可以使用“空白标识符”`_`来接收一个变量的分配，并丢弃该变量。

空白标识符可以被赋值任何类型的值，它经常被用在有多个返回值的函数。

### 多返回值

```go
func SumProduct(a, b int) (int, int) {
	return a + b, a * b
}

func main() {
	// 只需要sum
	sum, _ := SumProduct(1, 2) // 乘积会被丢弃
	fmt.Println(sum)           // -> 3
}
```

### 跟`range`一起用

```go
func main() {
	pets := []string{"dog", "cat", "fish"}

	// range 会同时返回index和value
	// 但有时候我们只想用其中之一
	for _, pet := range pets {
		fmt.Println(pet)
	}
}
```

{% hint style="info" %}

#### 译者注

简言之，空白标识符就是个特殊变量名，但凡赋给这个变量名的变量值都会被丢弃。
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://denglj.gitbook.io/essential-go/03-variables/blank-identifier.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
