> 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/01-getting-started/mac-os-install-and-setup.md).

# macOS安装和设置

## 使用官方安装包

从 <https://golang.org/dl/> 下载`.pkg`文件执行安装即可。&#x20;

## 使用Homebrew安装

* 如果您还没安装好Homebrew，请按照[说明](https://brew.sh/)安装它。
* `brew install go`

## 设置

安装好编译器之后需要设置[`GOPATH`](/essential-go/01-getting-started/gopath-goroot-gobin.md)环境变量。

从Go v1.8起，`GOPATH`环境变量默认为`$HOME/go`，您可以跳过设置。

创建Go目录 `mkdir $HOME/go`。

然后在`~/.bash_profile`文件中添加如下两行：

```
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
```

要使配置生效请执行`source ~/.bash_profile`，或重启终端。

## 解释

`~/.bash_profile`是`bash`在启动时会执行的文件。

通过在其中添加命令，我们可以每个`shell`会话中使用它们。

**译者注**：如果您使用的是`zsh` shell，应该在`~/.zsh`文件中添加设置。

您应该了解[`GOPATH`对go工具的影响](/essential-go/01-getting-started/gopath-goroot-gobin.md)。

{% hint style="success" %}

#### 阿驹补遗

可以打开 Cmd 或 Powershell 终端，执行`go env`命令，以查看Go编译器支持的环境变量。一般的，还需要注意的环境变量为：

* `GOBIN` ，go get 和 go install 安装可执行文件的位置
* `GOROOT`，go编译器安装的根目录
* `GOPROXY`，go包下载代理，国内用户可设为 <https://goproxy.cn,direct>
  {% endhint %}
