Installing Go from source 从源安装Go

Introduction 介绍

Go is an open source project, distributed under a BSD-style license . Go是一个开源项目,根据BSD样式的许可证发行 . This document explains how to check out the sources, build them on your own machine, and run them. 本文档说明了如何检出源代码,如何在自己的计算机上构建源代码以及如何运行它们.

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 toolchains. 有两个官方的Go编译器工具链. This document focuses on the gc Go compiler and tools. 本文档重点介绍gc Go编译器和工具. For information on how to work on gccgo , a more traditional compiler using the GCC back end, see Setting up and using gccgo . 有关如何使用gccgo (使用GCC后端的更传统的编译器)的信息,请参阅《 设置和使用gccgo》 .

The Go compilers support nine instruction sets. Go编译器支持九个指令集. There are important differences in the quality of the compilers for the different architectures. 不同体系结构的编译器质量存在重要差异.

amd64 (also known as x86-64 ) amd64 (也称为x86-64
A mature implementation. 成熟的实现.
386 ( x86 or x86-32 ) 386x86x86-32
Comparable to the amd64 port. 相当于amd64端口.
arm ( ARM ) armARM
Supports Linux, FreeBSD, NetBSD, OpenBSD and Darwin binaries. 支持Linux,FreeBSD,NetBSD,OpenBSD和Darwin二进制文件. Less widely used than the other ports. 不如其他端口广泛使用.
arm64 ( AArch64 ) arm64AArch64
Supports Linux and Darwin binaries. 支持Linux和Darwin二进制文件. New in 1.5 and not as well exercised as other ports. 1.5中的新功能,没有其他端口那么好用.
ppc64, ppc64le (64-bit PowerPC big- and little-endian) ppc64, ppc64le (64位PowerPC大端和小端)
Supports Linux binaries. 支持Linux二进制文件. New in 1.5 and not as well exercised as other ports. 1.5中的新功能,没有其他端口那么好用.
mips, mipsle (32-bit MIPS big- and little-endian) mips, mipsle (32位MIPS大端和小端)
Supports Linux binaries. 支持Linux二进制文件. New in 1.8 and not as well exercised as other ports. 1.8中的新功能,没有其他端口那么好用.
mips64, mips64le (64-bit MIPS big- and little-endian) mips64, mips64le (64位MIPS大端和小端)
Supports Linux binaries. 支持Linux二进制文件. New in 1.6 and not as well exercised as other ports. 1.6版中的新功能,没有其他端口那么好用.
s390x (IBM System z) s390x (IBM System z)
Supports Linux binaries. 支持Linux二进制文件. New in 1.7 and not as well exercised as other ports. 1.7中的新功能,没有其他端口那么好用.
wasm (WebAssembly) wasm (WebAssembly)
Targets the WebAssembly platform. 针对WebAssembly平台. New in 1.11 and not as well exercised as other ports. 1.11中的新增功能,没有其他端口那么好用.

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. 除了底层操作系统接口代码之类的内容外,所有端口的运行时支持都是相同的,包括标记清除垃圾收集器,有效的数组和字符串切片以及对有效的goroutine(例如堆栈)的支持.按需增长和收缩.

The compilers can target the DragonFly BSD, FreeBSD, Linux, NetBSD, OpenBSD, macOS (Darwin), Plan 9, Solaris and Windows operating systems. 编译器可以针对DragonFly BSD,FreeBSD,Linux,NetBSD,OpenBSD,macOS(Darwin),Plan 9,Solaris和Windows操作系统. The full set of supported combinations is listed in the discussion of environment variables below. 在下面的环境变量讨论中列出了支持的全套组合.

See the main installation page for the overall system requirements . 有关整体系统要求,请参见主安装页面. The following additional constraints apply to systems that can be built only from source: 以下附加约束适用于只能从源构建的系统:

  • For Linux on PowerPC 64-bit, the minimum supported kernel version is 2.6.37, meaning that Go does not support CentOS 6 on these systems. 对于PowerPC 64位上的Linux,最低支持的内核版本是2.6.37,这意味着Go在这些系统上不支持CentOS 6.

Install Go compiler binaries 安装Go编译器二进制文件

The Go toolchain is written in Go. Go工具链是用Go编写的. To build it, you need a Go compiler installed. 要构建它,您需要安装Go编译器. The scripts that do the initial build of the tools look for an existing Go tool chain in $GOROOT_BOOTSTRAP . 进行工具初始构建的脚本在$GOROOT_BOOTSTRAP寻找现有的Go工具链. If unset, the default value of GOROOT_BOOTSTRAP is $HOME/go1.4 . 如果未设置,则GOROOT_BOOTSTRAP的默认值为$HOME/go1.4 .

There are many options for the bootstrap toolchain. 引导工具链有很多选项. After obtaining one, set GOROOT_BOOTSTRAP to the directory containing the unpacked tree. 获得一个树后,将GOROOT_BOOTSTRAP设置到包含解压缩树的目录. For example, $GOROOT_BOOTSTRAP/bin/go should be the go command binary for the bootstrap toolchain. 例如, $GOROOT_BOOTSTRAP/bin/go应该是引导工具链的go命令二进制文件.

Bootstrap toolchain from binary release 二进制发行版的Bootstrap工具链

To use a binary release as a bootstrap toolchain, see the downloads page or use any other packaged Go distribution. 要将二进制发行版用作引导工具链,请参见下载页面或使用任何其他打包的Go发行版.

Bootstrap toolchain from source 来自源代码的Bootstrap工具链

To build a bootstrap toolchain from source, use either the git branch release-branch.go1.4 or go1.4-bootstrap-20171003.tar.gz , which contains the Go 1.4 source code plus accumulated fixes to keep the tools running on newer operating systems. 要从源代码构建引导工具链,请使用git分支release-branch.go1.4go1.4-bootstrap-20171003.tar.gz ,其中包含Go 1.4源代码以及累积的修补程序,以使工具在较新的版本上运行操作系统. (Go 1.4 was the last distribution in which the toolchain was written in C.) After unpacking the Go 1.4 source, cd to the src subdirectory, set CGO_ENABLED=0 in the environment, and run make.bash (or, on Windows, make.bat ). (Go 1.4是使用C编写工具链的最后一个发行版.)解CGO_ENABLED=0 Go 1.4源代码后,将cd CGO_ENABLED=0 src子目录,在环境中设置CGO_ENABLED=0并运行make.bash (或者在Windows上,运行make.bat ).

Once the Go 1.4 source has been unpacked into your GOROOT_BOOTSTRAP directory, you must keep this git clone instance checked out to branch release-branch.go1.4 . 将Go 1.4源代码解压缩到您的GOROOT_BOOTSTRAP目录后,您必须将此git clone实例检出到分支release-branch.go1.4 . Specifically, do not attempt to reuse this git clone in the later step named "Fetch the repository." 具体来说,请勿在稍后的名为"获取存储库"的步骤中尝试重用此git克隆. The go1.4 bootstrap toolchain must be able to properly traverse the go1.4 sources that it assumes are present under this repository root. go1.4引导程序工具链必须能够正确遍历它假定存在于此存储库根目录下的go1.4源.

Bootstrap toolchain from cross-compiled source 来自交叉编译源的Bootstrap工具链

To cross-compile a bootstrap toolchain from source, which is necessary on systems Go 1.4 did not target (for example, linux/ppc64le ), install Go on a different system and run bootstrap.bash . 要从源代码交叉编译引导工具链,这在Go 1.4未定位的系统上是必需的(例如linux/ppc64le ),请在其他系统上安装Go并运行bootstrap.bash .

When run as (for example) 当运行为(例如)

 $ GOOS=linux GOARCH=ppc64 ./bootstrap.bash $ GOOS = linux GOARCH = ppc64 ./bootstrap.bash

bootstrap.bash cross-compiles a toolchain for that GOOS/GOARCH combination, leaving the resulting tree in ../../go-${GOOS}-${GOARCH}-bootstrap . bootstrap.bash交叉编译该GOOS/GOARCH组合的工具链,将生成的树保留在../../go-${GOOS}-${GOARCH}-bootstrap . That tree can be copied to a machine of the given target type and used as GOROOT_BOOTSTRAP to bootstrap a local build. 该树可以复制到给定目标类型的计算机上,并用作GOROOT_BOOTSTRAP来引导本地构建.

Bootstrap toolchain using gccgo 使用gccgo的Bootstrap工具链

To use gccgo as the bootstrap toolchain, you need to arrange for $GOROOT_BOOTSTRAP/bin/go to be the go tool that comes as part of gccgo 5. For example on Ubuntu Vivid: 要将gccgo用作引导工具链,您需要安排$GOROOT_BOOTSTRAP/bin/go成为gccgo 5附带的go工具.例如,在Ubuntu Vivid上:

 $ sudo apt-get install gccgo-5 $ sudo apt-get install gccgo-5
$ sudo update-alternatives --set go /usr/bin/go-5 $ sudo update-alternatives --set go / usr / bin / go-5
$ GOROOT_BOOTSTRAP=/usr ./make.bash $ GOROOT_BOOTSTRAP = / usr ./make.bash

Install Git, if needed 如果需要,安装Git

To perform the next step you must have Git installed. 要执行下一步,您必须安装Git. (Check that you have a git command before proceeding.) (继续之前,请检查您是否具有git命令.)

If you do not have a working Git installation, follow the instructions on the Git downloads page. 如果没有有效的Git安装,请按照Git下载页面上的说明进行操作.

(Optional) Install a C compiler (可选)安装C编译器

To build a Go installation with cgo support, which permits Go programs to import C libraries, a C compiler such as gcc or clang must be installed first. 要构建具有cgo支持的Go安装程序,该程序允许Go程序导入C库,必须首先安装gccclang类的C编译器. Do this using whatever installation method is standard on the system. 使用系统上标准的任何安装方法来执行此操作.

To build without cgo , set the environment variable CGO_ENABLED=0 before running all.bash or make.bash . 要在没有cgo情况下cgo构建,请在运行all.bashmake.bash之前设置环境变量CGO_ENABLED=0 .

Fetch the repository 获取存储库

Change to the directory where you intend to install Go, and make sure the goroot directory does not exist. 转到您打算安装Go的目录,并确保goroot目录不存在. Then clone the repository and check out the latest release tag ( go1.12 , for example): 然后克隆存储库并签出最新的发行版标记(例如go1.12 ):

 $ git clone https://go.googlesource.com/go goroot $ git clone https://go.googlesource.com/go goroot
$ cd goroot $ cd goroot
$ git checkout <tag> $ git checkout <标签>

Where <tag> is the version string of the release. 其中, <tag>是发行版的版本字符串.

Go will be installed in the directory where it is checked out. Go将安装在检出目录中. For example, if Go is checked out in $HOME/goroot , executables will be installed in $HOME/goroot/bin . 例如,如果在$HOME/goroot检出Go,则可执行文件将安装在$HOME/goroot/bin . The directory may have any name, but note that if Go is checked out in $HOME/go , it will conflict with the default location of $GOPATH . 该目录可以具有任何名称,但是请注意,如果在$HOME/go检出Go,它将与$GOPATH的默认位置发生冲突. See GOPATH below. 请参阅下面的GOPATH .

Reminder: If you opted to also compile the bootstrap binaries from source (in an earlier section), you still need to git clone again at this point (to checkout the latest <tag> ), because you must keep your go1.4 repository distinct. 提醒:如果您还选择从源代码编译引导二进制文件(在前面的部分中),那么此时仍需要再次git clone (以检出最新的<tag> ),因为您必须保持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. 如果您打算修改go源代码,并项目贡献所做的更改 ,则将存储库从release分支移到master(开发)分支. Otherwise, skip this step. 否则,请跳过此步骤.

 $ git checkout master $ git checkout master

Install Go 安装Go

To build the Go distribution, run 要构建Go发行版,请运行

 $ cd src $ cd src
$ ./all.bash $ ./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. 在/ home / you / go中安装了Go for linux / amd64.
Installed commands in /home/you/go/bin. 在/ home / you / go / bin中安装的命令.
*** You need to add /home/you/go/bin to your $PATH. ***您需要将/ home / you / go / bin添加到$ 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. all.bash (或all.bat )为Go运行重要的测试,这比简单地构建Go需要更多的时间. If you do not want to run the test suite use make.bash (or make.bat ) instead. 如果您不想运行测试套件,请使用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" 导入" fmt"

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

Then run it with the go tool: 然后使用go工具运行它:

 $ go run hello.go $运行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. 您只需要多做一些设置即可.

How to Write Go Code Learn how to set up and use the Go tools 如何编写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 . 几个Go工具(包括godoc )的源代码保存在go.tools存储库中 . To install one of the tools ( godoc in this case): 要安装其中一种工具(本例中为godoc ):

 $ go get golang.org/x/tools/cmd/godoc $ go获取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; 您还必须设置一个工作区( GOPATH ); see How to Write Go Code for the details. 有关详细信息,请参见如何编写Go代码 .

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. 常用的社区资源,例如Freenode IRC服务器上的#go-nutsGo Nuts邮件列表,都有活跃的开发人员,可以帮助您解决安装或开发工作中的问题. 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. 对于那些希望保持最新状态的人,还有另一个邮件列表golang-checkins ,它接收一条消息,总结对Go存储库的每个签入的信息.

Bugs can be reported using the Go issue tracker . 可以使用Go问题跟踪器报告错误.

Keeping up with releases 跟上发布

New releases are announced on the golang-announce mailing list. 在golang-announce邮件列表中宣布了新版本. Each announcement mentions the latest release tag, for instance, go1.9 . 每个公告都提到最新的发行标签,例如go1.9 .

To update an existing tree to the latest release, you can run: 要将现有树更新为最新版本,可以运行:

 $ cd go/src $ cd go / src
$ git fetch $ git获取
$ git checkout <tag> $ git checkout <标签> 
 $ ./all.bash $ ./all.bash 
 

Where <tag> is the version string of the release. 其中, <tag>是发行版的版本字符串.

Optional environment variables 可选环境变量

The Go compilation environment can be customized by environment variables. Go编译环境可以通过环境变量进行定制. None is required by the build , but you may wish to set some to override the defaults. 构建不需要任何设置,但是您可能希望设置一些设置以覆盖默认设置.

Note that $GOARCH and $GOOS identify the target environment, not the environment you are running on. 请注意, $GOARCH$GOOS标识目标环境,而不是您正在运行的环境. 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 . 按体系结构,我们的意思是目标环境可以运行的二进制文件类型:运行仅32位操作系统的x86-64系统必须将GOARCH设置为386 ,而不是amd64 .

If you choose to override the defaults, set these variables in your shell profile ( $HOME/.bashrc , $HOME/.profile , or equivalent). 如果选择覆盖默认值,请在外壳程序配置文件( $HOME/.bashrc$HOME/.profile或等效文件)中设置这些变量. The settings might look something like this: 设置可能如下所示:

 export GOARCH=amd64 出口GOARCH = amd64
export GOOS=linux 导出GOOS = linux

although, to reiterate, none of these variables needs to be set to build, install, and develop the Go tree. 尽管重申一下,这些变量都不需设置即可构建,安装和开发Go树.

by  ICOPY.SITE