...

从源码安装Go

Introduction

引言

Go is an open source project, distributed under a BSD-style license. This document explains how to check out the sources, build them on your own machine, and run them.

Go是一个开源项目,在BSD风格授权下发行。 本文档说明了如何检出源码,在你的机器上构建并运行它们。

Most users don't need to do this, and will instead install from precompiled binary packages as described in Getting Started, a much simpler process. If you want to help develop what goes into those precompiled packages, though, read on.

大多数用户无需这些,而是按照起步 中描述更简单的步骤通过预编译二进制包来安装。 若你想进入那些预编译包以帮助开发,那么,往下看。

There are two official Go compiler tool chains. This document focuses on the gc Go compiler and tools (6g, 8g etc.). For information on how to work on gccgo, a more traditional compiler using the GCC back end, see Setting up and using gccgo.

官方有两个Go编译器工具链。本文档着重于 gc Go编译器及其工具 (6g8g 等)。gccgo 是一个更传统的的编译器, 它使用GCC作为后端。关于如何使用它工作的信息,请访问设置并使用gccgo

The Go compilers support three instruction sets. There are important differences in the quality of the compilers for the different architectures.

Go编译器支持三种指令集,以下为在不同的架构上,编译器特性中重要的不同之处。

amd64 (a.k.a. x86-64); 6g,6l,6c,6a
A mature implementation. The compiler has an effective optimizer (registerizer) and generates good code (although gccgo can do noticeably better sometimes).
386 (a.k.a. x86 or x86-32); 8g,8l,8c,8a
Comparable to the amd64 port.
arm (a.k.a. ARM); 5g,5l,5c,5a
Supports Linux, FreeBSD and NetBSD binaries. Less widely used than the other ports.
amd64(即 x86-64):6g,6l,6c,6a
一个成熟的实现。此编译器拥有高效的优化器(寄存优化器)并能生成高质量的代码 (当然,在某些事情上gccgo做得更好)。
386(即 x86x86-32):8g,8l,8c,8a
amd64 端口有可比之处。
arm(即 ARM):5g,5l,5c,5a
只支持Linux二进制。与其他接口相比较少使用,因此并没有经过充分的测试。

Except for things like low-level operating system interface code, the run-time support is the same in all ports and includes a mark-and-sweep garbage collector, efficient array and string slicing, and support for efficient goroutines, such as stacks that grow and shrink on demand.

除了像低级操作系统接口代码这类东西外,其运行时支持在所有接口中都相同,还包括 标记-清除 垃圾回收器,高效的数组与字符串切片,并支持高效的Go程,例如按需求扩展或收缩栈。

The compilers can target the DragonFly BSD, FreeBSD, Linux, NetBSD, OpenBSD, OS X (Darwin), Plan 9, Solaris and Windows operating systems. The full set of supported combinations is listed in the discussion of environment variables below.

该编译器可将DragonFly BSD、FreeBSD、Linux、NetBSD、OpenBSD、OS X (Darwin)、Plan 9、 Solaris以及Windows作为目标。它所支持组合的完整集将在下文的环境变量讨论中列出。

Install Go compiler binaries

安装 Go 编译器二进制文件

The Go tool chain is written in Go. To build it, you need a Go compiler installed. The scripts that do the initial build of the tools look for an existing Go tool chain in $HOME/go1.4. (This path may be overridden by setting the GOROOT_BOOTSTRAP environment variable.)

Go 工具链用 Go 编写,你需要安装一个 Go 编译器来构建它。用于初始化构建工具的脚本会在 $HOME/go1.4 中寻找既有的 Go 工具链。(该路径可通过设置 GOROOT_BOOTSTRAP 环境变量来覆盖。)

Build the tools with Go version 1.4 or a point release (1.4.1, 1.4.2 etc.). Go 1.4 binaries can be found at the downloads page.

构建这些工具需要 Go 1.4 或以上的发型版(1.4.1、1.4.2 等)。Go 1.4 二进制文件可在 此下载页面找到

Download the zip or tarball of Go 1.4 for your platform and extract it to $HOME/go1.4 (or your nominated GOROOT_BOOTSTRAP location).

请根据你的平台下载 Go 1.4 的 zip 或 tar 包,并将其提取到 $HOME/go1.4(或你指定的 GOROOT_BOOTSTRAP 位置)。

Install Git, if needed

需要时安装 Git

To perform the next step you must have Git installed. (Check that you have a git command before proceeding.)

要完成下一步,你必须安装 Git。(继续前请检查你是否有 git 命令。)

If you do not have a working Git installation, follow the instructions on the Git downloads page.

若你没有可用的 Git,请按照 Git 下载页面 的说明进行操作。

Fetch the repository

获取代码仓库

Go will install to a directory named go. Change to the directory that will be its parent and make sure the go directory does not exist. Then clone the repository and check out the latest release tag:

Go 将安装到名为 go 的目录。 切换到你想要安装 Go 的目录并确认该目录下没有 go 目录。 然后 clone 代码库并检出最新的发行版标签:

$ git clone https://go.googlesource.com/go
$ cd go
$ git checkout go1.4

If you intend to modify the go source code, and contribute your changes to the project, then move your repository off the release branch, and onto the master (development) branch. Otherwise, skip this step.

若你打算修改 go 源码并向此项目贡献你的更改, 请离开发行版分支并切换到 master(开发)分支上。否者请跳过这一步。

$ git checkout master

Install Go

安装 Go

To build the Go distribution, run

要构建 Go 发行版,请运行

$ cd src
$ ./all.bash

(To build under Windows use all.bat.)

(要在 Windows 下构建,请使用 all.bat。)

If all goes well, it will finish by printing output like:

若一切顺利,它将结束于打印输出,类似与:

ALL TESTS PASSED

---
Installed Go for linux/amd64 in /home/you/go.
Installed commands in /home/you/go/bin.
*** You need to add /home/you/go/bin to your $PATH. ***

where the details on the last few lines reflect the operating system, architecture, and root directory used during the install.

其中最后几行信息反映了操作系统、架构以及在安装中使用的根目录。

For more information about ways to control the build, see the discussion of environment variables below. all.bash (or all.bat) runs important tests for Go, which can take more time than simply building Go. If you do not want to run the test suite use make.bash (or make.bat) instead.

更多有关控制构建方式的信息,见下文关于环境变量的讨论。 all.bash(或 all.bat)会为Go运行重要的测试, 它比简单地构建Go要花更多的时间。若你不想运行测试套件,请使用 make.bash(或 make.bat)代替。

Testing your installation

测试你的安装

Check that Go is installed correctly by building a simple program.

通过构建一个简单的程序来检查Go的安装是否正确。

Create a file named hello.go and put the following program in it:

创建一个名为 hello.go 的文件,并将以下代码保存在其中:

package main

import "fmt"

func main() {
    fmt.Printf("hello, world\n")
}

Then run it with the go tool:

接着通过 go 工具运行它:

$ go run hello.go
hello, world

If you see the "hello, world" message then Go is installed correctly.

若你看到了“hello, world”信息,那么你的Go已被正确安装。

Set up your work environment

设置你的工作环境变量

You're almost done. You just need to do a little more setup.

差不多了,你只要再设置一下就好。

如何使用Go编程 学习如何设置并使用Go工具

The How to Write Go Code document provides essential setup instructions for using the Go tools.

如何使用Go编程提供了使用Go工具的基本设置说明

Install additional tools

安装附加工具

The source code for several Go tools (including godoc) is kept in the go.tools repository. To install all of them, run the go get command:

某些 Go 工具(包括 godoc)保存在 go.tools 源码库中。 要安装所有的这些工具,请运行 go get 命令:

$ go get golang.org/x/tools/cmd/...

Or if you just want to install a specific command (godoc in this case):

若你想安装具体的命令(此处例为 godoc),请执行:

$ go get golang.org/x/tools/cmd/godoc

To install these tools, the go get command requires that Git be installed locally.

要安装这些工具,go get 命令需要本地安装了 Git

You must also have a workspace (GOPATH) set up; see How to Write Go Code for the details.

你必须还设置了工作空间(GOPATH);详情见 如何使用Go编程

Note: The go command will install the godoc binary to $GOROOT/bin (or $GOBIN) and the cover and vet binaries to $GOROOT/pkg/tool/$GOOS_$GOARCH. You can access the latter commands with "go tool cover" and "go tool vet".

go 命令会将 godoc 程序安装到 $GOROOT/bin(或 $GOBIN)中,而 covervet 则会被安装到 $GOROOT/pkg/tool/$GOOS_$GOARCH 中。 你可以通过 “go tool cover” 和 “go tool vet” 来访问它们。

Community resources

社区资源

The usual community resources such as #go-nuts on the Freenode IRC server and the Go Nuts mailing list have active developers that can help you with problems with your installation or your development work. For those who wish to keep up to date, there is another mailing list, golang-checkins, that receives a message summarizing each checkin to the Go repository.

一般的社区资源例如Freenode IRC服务器上的 #go-nuts 频道以及Go Nuts 邮件列表有一些活跃的开发者可以帮助你的安装或开发工作问题。对于那些希望保持最新的人,有另一个邮件列表 golang-checkins,它接收每一次对Go仓库的签入信息摘要。

Bugs can be reported using the Go issue tracker.

Bug应当使用Go问题跟踪系统报告。

保持最新的发行版

New releases are announced on the golang-announce mailing list. Each announcement mentions the latest release tag, for instance, go1.4.

新的发行版将在golang-announce 邮件列表中宣布。每次宣布都会提及最新的发型标签,例如 go1.4

To update an existing tree to the latest release, you can run:

要将现有源码树更新到最新发行版,你可以运行:

$ cd go/src
$ git fetch
$ git checkout <tag>
$ ./all.bash
Where <tag> is the version string of the release.

Optional environment variables

可选环境变量

The Go compilation environment can be customized by environment variables. None is required by the build, but you may wish to set some to override the defaults.

Go编译环境可通过环境变量定制。构建无需它们,但你或许希望设置一些来覆盖默认的值。

  • $GOROOT

    The root of the Go tree, often $HOME/go. Its value is built into the tree when it is compiled, and defaults to the parent of the directory where all.bash was run. There is no need to set this unless you want to switch between multiple local copies of the repository.

  • $GOROOT

    Go树的根,一般为 $HOME/go。其值在编译时构建成树,默认为 all.bash 所运行的目录的父目录。它一般无需设置, 除非你想在代码库的多个本地副本之间选择。

  • $GOROOT_FINAL

    The value assumed by installed binaries and scripts when $GOROOT is not set explicitly. It defaults to the value of $GOROOT. If you want to build the Go tree in one location but move it elsewhere after the build, set $GOROOT_FINAL to the eventual location.

  • $GOROOT_FINAL

    $GOROOT 未显式地设置时,其值由已安装二进制文件及脚本假定。 它默认为 $GOROOT 的值。若你想在某个位置构建Go树而不在构建后移动它, 那么就将 $GOROOT_FINAL 设置为最后的位置。

  • $GOOS and $GOARCH

    The name of the target operating system and compilation architecture. These default to the values of $GOHOSTOS and $GOHOSTARCH respectively (described below).

  • $GOOS$GOARCH

    目标操作系统与编译架构的名字。它们分别默认为 $GOHOSTOS$GOHOSTARCH 的值(将在下文描述)。

    Choices for $GOOS are darwin (Mac OS X 10.6 and above), dragonfly, freebsd, linux, netbsd, openbsd, plan9, solaris and windows. Choices for $GOARCH are amd64 (64-bit x86, the most mature port), 386 (32-bit x86), and arm (32-bit ARM). The valid combinations of $GOOS and $GOARCH are:

    $GOOS 的选项为 darwin(Mac OS X 10.6及以上)、dragonflyfreebsdlinuxnetbsdopenbsdplan9solaris 以及 windows$GOARCH 的选项为 amd64(64位x86,最成熟的端口)、 386(32位x86)以及 arm(32位ARM)。 $GOOS$GOARCH 的有效组合为:

    $GOOS $GOARCH
    darwin 386
    darwin amd64
    dragonfly 386
    dragonfly amd64
    freebsd 386
    freebsd amd64
    freebsd arm
    linux 386
    linux amd64
    linux arm
    netbsd 386
    netbsd amd64
    netbsd arm
    openbsd 386
    openbsd amd64
    plan9 386
    plan9 amd64
    solaris amd64
    windows 386
    windows amd64
  • $GOHOSTOS and $GOHOSTARCH

    The name of the host operating system and compilation architecture. These default to the local system's operating system and architecture.

  • $GOHOSTOS and $GOHOSTARCH

    宿主操作系统与编译架构的名字。它们默认为本地系统的操作系统与架构。

    Valid choices are the same as for $GOOS and $GOARCH, listed above. The specified values must be compatible with the local system. For example, you should not set $GOHOSTARCH to arm on an x86 system.

    其有效选项与 $GOOS 以及 $GOARCH 相同,已在上文列出。 其指定的值必须与本地系统兼容。例如在x86系统上,你不能将 $GOHOSTARCH 设置为 arm

  • $GOBIN

    The location where Go binaries will be installed. The default is $GOROOT/bin. After installing, you will want to arrange to add this directory to your $PATH, so you can use the tools. If $GOBIN is set, the go command installs all commands there.

  • $GOBIN

    Go二进制文件将安装的位置。它默认为 $GOROOT/bin 。 在安装之后,你会想要为整理而将此目录添加到你的 $PATH 中, 此时你可以使用此工具。若 $GOBIN 已经设置, 那么go命令会把所有命名都安装到这儿。

  • $GO386 (for 386 only, default is auto-detected if built on either 386 or amd64, 387 otherwise)

    This controls the code generated by 8g to use either the 387 floating-point unit (set to 387) or SSE2 instructions (set to sse2) for floating point computations.

  • $GO386(仅用于 386,若为原生构建,则默认自动决定,否则为 387

    它用于控制 8g 的代码生成,具体为387浮点运算单元(设置为 387), 或用于浮点数计算的 SSE2 指令(设置为 sse2)。

  • $GOARM (for arm only; default is auto-detected if building on the target processor, 6 if not)

    This sets the ARM floating point co-processor architecture version the run-time should target. If you are compiling on the target system, its value will be auto-detected.

    If in doubt, leave this variable unset, and adjust it if required when you first run the Go executable. The GoARM page on the Go community wiki contains further details regarding Go's ARM support.

  • $GOARM(仅用于 arm;若为原生构建,则默认自动决定,否则为 6)

    它设置了运行时目标的ARM浮点协助处理器架构的版本。若你在目标系统上编译,它将自动决定。

    若你不能确定,请保持此变量为未设置状态。当你第一次运行Go的可执行程序时,若有需要,它会自动调整。 Go社区维基GoARM 页面包含有关Go ARM支持更详细的信息。

    Note that $GOARCH and $GOOS identify the target environment, not the environment you are running on. In effect, you are always cross-compiling. By architecture, we mean the kind of binaries that the target environment can run: an x86-64 system running a 32-bit-only operating system must set GOARCH to 386, not amd64.

    注意 $GOARCH$GOOS 标识了目标环境, 而不是你正在运行的环境。实际上,你总是跨平台编译。通过此架构, 我们想要目标环境可以运行的二进制种类:x86-64系统运行32位的操作系统必须将 GOARCH 设置为 386 而非 amd64

    If you choose to override the defaults, set these variables in your shell profile ($HOME/.bashrc, $HOME/.profile, or equivalent). The settings might look something like this:

    若你选择覆盖默认值,需要在你的shell配置($HOME/.bashrc$HOME/.profile、或等价文件)中设置这些变量。该设置看起来可能像这样:

    export GOROOT=$HOME/go
    export GOARCH=amd64
    export GOOS=linux
    

    although, to reiterate, none of these variables needs to be set to build, install, and develop the Go tree.

    再次重申一遍,构建、安装并开发Go树无需设置这些变量。