天天看點

golang源碼安裝

Installing Go from source

Introduction

Install Go compiler binaries

Install Git, if needed

(Optional) Install a C compiler

Fetch the repository

(Optional) Switch to the master branch

Install Go

Testing your installation

Set up your work environment

Install additional tools

Community resources

Keeping up with releases

Optional environment variables

Your download should begin shortly. If it does not, click this link.

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.

Most users don't need to do this, and will instead install from precompiled binary packages as described inGetting 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 <code>gc</code> Go compiler and tools. For information on how to work on <code>gccgo</code>, a more traditional compiler using the GCC back end, see Setting up and using gccgo.

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

<code>amd64</code> (also known as <code>x86-64</code>)

A mature implementation. New in 1.7 is its SSA-based back end that generates compact, efficient code.

<code>386</code> (<code>x86</code> or <code>x86-32</code>)

Comparable to the <code>amd64</code> port, but does not yet use the SSA-based back end. It has an effective optimizer (registerizer) and generates good code (although <code>gccgo</code> can do noticeably better sometimes).

<code>arm</code> (<code>ARM</code>)

Supports Linux, FreeBSD, NetBSD and Darwin binaries. Less widely used than the other ports.

<code>arm64</code> (<code>AArch64</code>)

Supports Linux and Darwin binaries. New in 1.5 and not as well exercised as other ports.

<code>ppc64, ppc64le</code> (64-bit PowerPC big- and little-endian)

Supports Linux binaries. New in 1.5 and not as well exercised as other ports.

<code>mips64, mips64le</code> (64-bit MIPS big- and little-endian)

Supports Linux binaries. New in 1.6 and not as well exercised as other ports.

<code>s390x</code> (IBM System z)

Supports Linux binaries. New in 1.7 and not as well exercised as other ports.

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.

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 ofenvironment 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.

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 <code>$GOROOT_BOOTSTRAP</code>. If unset, the default value of <code>GOROOT_BOOTSTRAP</code> is <code>$HOME/go1.4</code>.

There are many options for the bootstrap tool chain. After obtaining one, set <code>GOROOT_BOOTSTRAP</code> to the directory containing the unpacked tree. For example, <code>$GOROOT_BOOTSTRAP/bin/go</code> should be the <code>go</code> command binary for the bootstrap tool chain.

To use a binary release as a bootstrap tool chain, see the downloads page or use any other packaged Go distribution.

To build a bootstrap tool chain from source, use either the git branch <code>release-branch.go1.4</code> or go1.4-bootstrap-20161024.tar.gz, which contains the Go 1.4 source code plus accumulated fixes to keep the tools running on newer operating systems. (Go 1.4 was the last distribution in which the tool chain was written in C.)

To cross-compile a bootstrap tool chain from source, which is necessary on systems Go 1.4 did not target (for example, <code>linux/ppc64le</code>), install Go on a different system and run bootstrap.bash.

When run as (for example)

<code>bootstrap.bash</code> cross-compiles a toolchain for that <code>GOOS/GOARCH</code> combination, leaving the resulting tree in <code>../../go-${GOOS}-${GOARCH}-bootstrap</code>. That tree can be copied to a machine of the given target type and used as <code>GOROOT_BOOTSTRAP</code> to bootstrap a local build.

To use gccgo as the bootstrap toolchain, you need to arrange for <code>$GOROOT_BOOTSTRAP/bin/go</code> to be the go tool that comes as part of gccgo 5. For example on Ubuntu Vivid:

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

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

To build a Go installation with <code>cgo</code> support, which permits Go programs to import C libraries, a C compiler such as <code>gcc</code> or <code>clang</code> must be installed first. Do this using whatever installation method is standard on the system.

To build without <code>cgo</code>, set the environment variable <code>CGO_ENABLED=0</code> before running <code>all.bash</code> or <code>make.bash</code>.

Go will install to a directory named <code>go</code>. Change to the directory that will be its parent and make sure the <code>go</code>directory does not exist. Then clone the repository and check out the latest release tag (<code>go1.7.5</code>, for example):

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.

To build the Go distribution, run

(To build under Windows use <code>all.bat</code>.)

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

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. <code>all.bash</code>(or <code>all.bat</code>) 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 <code>make.bash</code> (or <code>make.bat</code>) instead.

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

Create a file named <code>hello.go</code> and put the following program in it:

Then run it with the <code>go</code> tool:

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

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

How to Write Go CodeLearn how to set up and use the Go tools

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

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

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

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

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

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

The usual community resources such as <code>#go-nuts</code> 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.

Bugs can be reported using the Go issue tracker.

New releases are announced on the golang-announce mailing list. Each announcement mentions the latest release tag, for instance, <code>go1.7.5</code>.

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

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.

<code>$GOROOT</code>

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

<code>$GOROOT_FINAL</code>

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

<code>$GOOS</code> and <code>$GOARCH</code>

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

Choices for <code>$GOOS</code> are <code>darwin</code> (Mac OS X 10.7 and above and iOS), <code>dragonfly</code>, <code>freebsd</code>, <code>linux</code>, <code>netbsd</code>, <code>openbsd</code>,<code>plan9</code>, <code>solaris</code> and <code>windows</code>. Choices for <code>$GOARCH</code> are <code>amd64</code> (64-bit x86, the most mature port), <code>386</code> (32-bit x86), <code>arm</code> (32-bit ARM), <code>arm64</code> (64-bit ARM), <code>ppc64le</code> (PowerPC 64-bit, little-endian), <code>ppc64</code> (PowerPC 64-bit, big-endian), <code>mips64le</code> (MIPS 64-bit, little-endian), and <code>mips64</code> (MIPS 64-bit, big-endian). The valid combinations of <code>$GOOS</code> and <code>$GOARCH</code> are:

<code>$GOOS</code>

<code>$GOARCH</code>

<code>android</code>

<code>arm</code>

<code>darwin</code>

<code>386</code>

<code>amd64</code>

<code>arm64</code>

<code>dragonfly</code>

<code>freebsd</code>

<code>linux</code>

<code>ppc64</code>

<code>ppc64le</code>

<code>mips64</code>

<code>mips64le</code>

<code>netbsd</code>

<code>openbsd</code>

<code>plan9</code>

<code>solaris</code>

<code>windows</code>

<code>$GOHOSTOS</code> and <code>$GOHOSTARCH</code>

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

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

<code>$GOBIN</code>

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

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

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

<code>GO386=387</code>: use x87 for floating point operations; should support all x86 chips (Pentium MMX or later).

<code>GO386=sse2</code>: use SSE2 for floating point operations; has better performance than 387, but only available on Pentium 4/Opteron/Athlon 64 or later.

<code>$GOARM</code> (for <code>arm</code> 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.

<code>GOARM=5</code>: use software floating point; when CPU doesn't have VFP co-processor

<code>GOARM=6</code>: use VFPv1 only; default if cross compiling; usually ARM11 or better cores (VFPv2 or better is also supported)

<code>GOARM=7</code>: use VFPv3; usually Cortex-A cores

Note that <code>$GOARCH</code> and <code>$GOOS</code> 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 <code>GOARCH</code> to <code>386</code>, not <code>amd64</code>.

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

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

繼續閱讀