天天看點

golang安裝

Getting Started

Install the Go tools

Test your installation

Uninstalling Go

Getting help

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

If you are upgrading from an older version of Go you must first remove the existing version.

Download the archive and extract it into <code>/usr/local</code>, creating a Go tree in <code>/usr/local/go</code>. For example:

(Typically these commands must be run as root or through <code>sudo</code>.)

Add <code>/usr/local/go/bin</code> to the <code>PATH</code> environment variable. You can do this by adding this line to your <code>/etc/profile</code>(for a system-wide installation) or <code>$HOME/.profile</code>:

The Go binary distributions assume they will be installed in <code>/usr/local/go</code> (or <code>c:\Go</code> under Windows), but it is possible to install the Go tools to a different location. In this case you must set the <code>GOROOT</code> environment variable to point to the directory in which it was installed.

For example, if you installed Go to your home directory you should add the following commands to <code>$HOME/.profile</code>:

Note: <code>GOROOT</code> must be set only when installing to a custom location.

Check that Go is installed correctly by setting up a workspace and building a simple program, as follows.

Create a directory to contain your workspace, <code>$HOME/work</code> for example, and set the <code>GOPATH</code> environment variable to point to that location.

You should put the above command in your shell startup script (<code>$HOME/.profile</code> for example).

Next, make the directories <code>src/github.com/user/hello</code> inside your workspace (if you use GitHub, substitute your user name for <code>user</code>), and inside the <code>hello</code> directory create a file named <code>hello.go</code> with the following contents:

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

The command above will put an executable command named <code>hello</code> (or <code>hello.exe</code>) inside the <code>bin</code> directory of your workspace. Execute the command to see the greeting:

If you see the "hello, world" message then your Go installation is working.

Before rushing off to write Go code please read the How to Write Go Code document, which describes some essential concepts about using the Go tools.

To remove an existing Go installation from your system delete the <code>go</code> directory. This is usually <code>/usr/local/go</code> under Linux, Mac OS X, and FreeBSD or <code>c:\Go</code> under Windows.

You should also remove the Go <code>bin</code> directory from your <code>PATH</code> environment variable. Under Linux and FreeBSD you should edit <code>/etc/profile</code> or <code>$HOME/.profile</code>. If you installed Go with the Mac OS X package then you should remove the <code>/etc/paths.d/go</code> file. Windows users should read the section about setting environment variables under Windows.

For real-time help, ask the helpful gophers in <code>#go-nuts</code> on the Freenode IRC server.

The official mailing list for discussion of the Go language is Go Nuts.

Report bugs using the Go issue tracker.

繼續閱讀