一、安装Go语言相关工具
sudo apt-get install bison ed gawk gcc libc6-dev make gccgo-5
二、获取Go语言源码包
下载后放到/usr/local/目录下并解压
$ tar -zxvf go1.6.2.linux-amd64.tar.gz
注:源码包解压后根目录的名字是 go,后面设置环境变量会用到。
三、配置Go语言环境变量
环境变量的配置不论是下一步中编译Go语言源码,还是在之后的开发过程中都至关重要
$ sudo vim ~/.bashrc export GOROOT=/usr/local/go export GOROOT_BOOTSTRAP=/usr/local/go export GOARCH=amd64 export GOOS=linux export GOBIN=$GOROOT/bin/ export GOTOOLS=$GOROOT/pkg/tool/ export PATH=$GOBIN:$GOTOOLS:$PATH export GOPATH=$HOME/golang/go
$ source ~/.bashrc
注:a. GOROOT是存放源码包的位置。比如,这里是放在/usr/local/目录下。
b. GOROOT_BOOTSTRAP在1.6编译时使用 Go 1.4 的Go compilers进行构建,则需指定go1.4的路径;--这里不进行编译。
c. GOARCH 与 GOOS 与下载的发行版有关,例如:我下载的是64位的Ubuntu,因此 GOARCH 为 amd64;如果是32位的Ubuntu,则填386;而GOOS 为 linux。
d. GOBIN为GOROOT路径下bin文件夹路径。
e. GOTOOLS路径在编译时会用到。
f. PATH路径很关键,指定GOBIN和GOTOOLS路径。
g. GOPATH为Go的工作目录,可以随时修改;这里,我选择将GOPATH指定到$HOME/golang/go路径下;则,要在$HOME目录下新建golang/go目录,作为Go的工作目录。
四、配置
接着输入go命令,如果看到如下信息,则即可使用Go语言了
$ go Go is a tool for managing Go source code. Usage: go command [arguments] The commands are: build compile packages and dependencies clean remove object files doc show documentation for package or symbol env print Go environment information fix run go tool fix on packages fmt run gofmt on package sources generate generate Go files by processing source get download and install packages and dependencies install compile and install packages and dependencies list list packages run compile and run Go program test test packages tool run specified go tool version print Go version vet run go tool vet on packages Use "go help [command]" for more information about a command. Additional help topics: c calling between Go and C buildmode description of build modes filetype file types gopath GOPATH environment variable environment environment variables importpath import path Syntax packages description of package lists testflag description of testing flags testfunc description of testing functions Use "go help [topic]" for more information about that topic.