# 1.4 项目设置

要使用 `master` 分支的代码，就需要能够编译最新代码的环境，下列步骤是在 Linux 系统下配置项目的简易步骤（详细步骤参见[官方教程](https://golang.org/doc/install/source)）：

1. 下载 Go 语言编译器，使用[最新发布版本](https://golang.org/dl/)即可。解压后将文件夹更名为 `gobs` 并移动到某目录下，例如 `$HOME/Tools` 下。此时 `$HOME/Tools/gobs` 包含着最新的 Go 预编译发行版本
2. &#x20;设置编译工具链环境变量：

   ```
   export GOROOT_BOOTSTRAP=$HOME/Tools/gobs
   ```

   该环境变量用于搜索编译 Go 源码时的编译器工具链，因为只设置了该环境变量，所以该目录下的 go 只会在后续的编译步骤中被搜索使用到
3. &#x20;Clone 源代码并编译：

   ```
   git clone https://github.com/golang/go $HOME/Tools/go # Clone 源代码到目标目录
   cd $HOME/Tools/go/src
   git checkout d26fc68aa10dc8eda5ccdcc80d790e7df2fd9823 # checkout 与本文一致的代码
   ./all.bash                  # 编译源代码
   ```

   完成编译之后`$HOME/Tools/go` 下面会多出`bin` 与`pkg` 目录。`bin` 目录下面包含可执行文件 `go`, 而`pkg` 目录下包含着各个库编译后的对象文件（pkg/linux\_amd64）、工具链（pkg/tool）以及构建时的缓存文件（pkg/obj/go-build）。可以运行 `bin/go version` 查看当前版本：

   ```
       ./go version
       go version devel +ce19612a36 Thu Mar 4 15:10:16 2021 +0800 linux/amd64
   ```

   > 注意：工程中的很多代码是在构建时自动生成的，因此如果读者重新更新了代码，一定要先运行脚本 `./all.bash`, 否则 IDE 可能会提示编译错误，或者无法运行某些 UT
4. &#x20;设置环境变量：

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

   注意这样设置之后整个系统都会使用 master 分支编译出来的 go, 如果你需要在本地编译生产环境的系统或者其他工具，则需要修改 `GOROOT`

编译器及整个工具链的代码在目 `$GOROOT/src/cmd/` 下面，该目录的结构也是一个典型的 go 语言工程，将该目录作为根目录引入 IDE 即可。

> 项目的配置方式不止一种，这里只是分享的基于作者自己开发环境（Archlinux）的个人配置，每个人偏爱的环境以及工具都不一样，只需要按照上述思路配置能够正常工作即可。


---

# Agent Instructions: 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://gocompiler.shizhz.me/1.-golang-bian-yi-qi-qian-yan/1.4-xiang-mu-she-zhi.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.
