> For the complete documentation index, see [llms.txt](https://wskdsgcf.gitbook.io/mastering-go-zh-cn/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://wskdsgcf.gitbook.io/mastering-go-zh-cn/10.0/10.1/10.1.1.md).

# 10.1.1 环境变量 GOMAXPROCS

这个 `GOMAXPROCS` 环境变量（和Go 函数）允许您限制操作系统线程数，它能同时执行用户级 Go 代码。Go 1.5 版本开始，`GOMAXPROCS` 的默认值应该是您 Unix 机器的内核数。

如果您决定分配给 `GOMAXPROCS` 的值小于您的 Unix 机器的内核数的话，可能会影响您的程序的性能。然而，使用比可用内核大的 `GOMAXPROCS` 值也一定不会使您的程序运行的快。

您可用通过编程的方式来发现 `GOMAXPROCS` 环境变量的值，即相关代码。在下面的 `maxprocs.go` 程序中找到：

```go
package main

import (
    "fmt"
    "runtime"
)

func getGOMAXPROCS() int {
    return runtime.GOMAXPROCS(0)
}

func main() {
    fmt.Printf("GOMAXPROCS:%d\n", getGOMAXPROCS())
}
```

在一台 Intel i7 处理器的机器上执行 `maxprocs.go` 产生如下输出：

```
$go run maxprocs.go
GOMAXPROCS: 8
```

然而，您可以在程序执行前，通过改变 `GOMAXPROCS` 环境变量的值来修改上面的输出。在 `bash(1)` Unix shell 里执行下面的命令：

```
$go version
go version go1.9.4 darwin/amd64
$export GOMAXPROCS=800; go run maxprocs.go
GOMAXPROCS: 800
$export GOMAXPROCS=4; go run maxprocs.go
GOMAXPROCS: 4
```


---

# 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://wskdsgcf.gitbook.io/mastering-go-zh-cn/10.0/10.1/10.1.1.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.
