安装go只需要在官网(https://golang.org/dl/)下载适合系统的二进制发布包,按照安装说明进行安装即可。
对于Linux, Mac OS X和FreeBSD系统下的安装,三步即可完成:
1.下载tarballs文件go$VERSION.$OS-$ARCH.tar.gz,例如1.8.3
wget https://storage.googleapis.com/golang/go1.8.3.linux-amd64.tar.gz)
2.执行tar -C /usr/local -xzf go$VERSION.$OS-$ARCH.tar.gz提取到 /usr/local
3.设置环境变量,将/usr/local/go/bin 加入到PATH环境变量。可以通过将
export PATH=$PATH:/usr/local/go/bin 添加到/etc/profile或$HOME/.profile文件的最后一行。
对于windows系统下的安装,可以下载msi安装包,安装程序自动将go发布拷贝到c:\go目录下,并且将c:\go\bin目录加入到PATH环境变量。
如果安装位置不是/usr/local/go或者c:\go目录,则需要设置GOROOT环境变量,将GOROOT/bin加入到PATH环境变量。
Go的一大特征是简单,通过下载二进制发布包安装go环境就充分体现了简单性。
某些时候,可能要使用特殊版本的go,从源码安装,这同样很简单。从源码安装,首先要进行编译。Go 1.5实现了bootstrapping,所以编译1.5以后的版本和编译1.5以前的版本会略有不同。bootstrapping是编译器领域的一个术语,中文翻译为自举或自展,通俗地讲“用要编译的目标语言编写其编译器(汇编器)”。Go1.5开始编译器和运行时完全用Go语言编写(还有少量汇编);go1.4是最后一个用C编写工具链的发布。
编译go1.4,ubuntu 16.04.2系统:
cd ~
wget https://storage.googleapis.com/golang/go1.4-bootstrap-20170531.tar.gz
tar zxvf go1.4-bootstrap-20170531.tar.gz
cd go/src/
./all.bash
编译完成后,可以看到如下输出:
ALL TESTS PASSED
Installed Go for linux/amd64 in /home/dell/go
Installed commands in /home/dell/go/bin
*** You need to add /home/dell/go/bin to your PATH.
成功编译后,将~/go/bin加入到PATH环境变量
sudo gedit /etc/profile
在最后一行加入 export PATH=/home/dell/go/bin:$PATH 保存
source /etc/profile
执行go version,检查安装
dell@dell-VirtualBox:~/go/src$ go version
go version go1.4-bootstrap-20170531 linux/amd64
编译Go1.5及以上版本:
Go 1.5开始编译器和运行时用go自身编写,要编译它们,首先要安装go编译器。all.bash 编译脚本会在$GOROOT_BOOTSTRAP环境变量中查找一个已经存在的go tool chain,实际上就是要有一个编译好的bin/go程序,$GOROOT_BOOTSTRAP/bin/go应该是go二进制命令。有很多选择,可以在官网(https://golang.org/dl/)下载go发布包;也可以用go1.4源码编译,也就是按照上面的步骤编译go1.4,然后再去编译更高版本的go。
用官方下载的go1.7编译go 1.8,ubuntu 16.04.2 举例:
cd ~
wget https://storage.googleapis.com/golang/go1.7.6.linux-amd64.tar.gz
wget https://github.com/golang/go/archive/go1.8.3.tar.gz
tar zxvf go1.7.6.linux-amd64.tar.gz
tar zxvf go1.8.3.tar.gz
export GOROOT_BOOTSTRAP=/home/dell/go
cd go-go1.8.3/src
./all.bash
编译成功后,有如下输出:
Go version is “go1.8.3”, ignoring -next /home/dell/go-go1.8.3/api/next.txt
ALL TESTS PASSED
Installed Go for linux/amd64 in /home/dell/go-go1.8.3
Installed commands in /home/dell/go-go1.8.3/bin
*** You need to add /home/dell/go-go1.8.3/bin to your PATH.
sudo gedit /etc/profile 将export PATH=/home/dell/go-go1.8.3/bin:$PATH 加入到文件最后一行
执行go version 检查安装
dell@dell-VirtualBox:~/go-go1.8.3/src$ go version
go version go1.8.3 linux/amd64
golang的源码安装其实比较简单,只需运行源码包中的脚本src/all.bash,等到出现类似以下字样就安装好了:
Installed Go for linux/amd64 in xxx(目录地址)
Installed commands in xxx(目录地址)
但是在源码安装1.5版本以上的go时会报以下的错误 :
cmd/dist
ERROR: Cannot find /home/fabric/go1.4/bin/go.
Set $GOROOT_BOOTSTRAP to a working Go tree >= Go 1.4.
这是由于go 1.5版以后的编译安装需要1.4版本go,所以如果想要通过源码方式安装高版本go,必须先安装好1.4版本的go
安装golang 1.9
主要操作如下:
我们需要编译好的golang环境支持c语言的文件,所以需要开启cgo
root@fabric:/opt/gopath/src/github.com/MangoDowner/go/src# export CGO_ENABLED=1
我们需要指定由go 1.4进行编译,所以得设置以下环境变量
root@fabric:/opt/gopath/src/github.com/MangoDowner/go/src# export GOROOT_BOOTSTRAP=/usr/local/go1.4
编译go之前需要设置环境变量,
GOROOT_BOOTSTRAP是go二进制文件夹的所在目录,
GO_GCFLAGS是编译go时使用的参数.
export GOROOT_BOOTSTRAP=~/git_go/go_bin
export GO_GCFLAGS=”-N -l”
这里的-N参数代表禁止优化, -l参数代表禁止内联, go在编译目标程序的时候会嵌入运行时(runtime)的二进制,
禁止优化和内联可以让运行时(runtime)中的函数变得更容易调试.
go1.9.2 mac 编译go1.10
1,cd /Users/didi/goLang/src/github.com/golang/go/src
$echo $GOROOT_BOOTSTRAP
$which go
/usr/local/go/bin/go
$sh all.bash
Building Go cmd/dist using /usr/local/go.
Building Go toolchain1 using /usr/local/go.
Building Go bootstrap cmd/go (go_bootstrap) using Go toolchain1.
Building Go toolchain2 using go_bootstrap and Go toolchain1.
Building Go toolchain3 using go_bootstrap and Go toolchain2.
Building packages and commands for darwin/amd64.
ok archive/tar 0.122s
ok archive/zip 3.967s
ok bufio 0.065s
ok bytes 1.635s
ok compress/bzip2 0.075s
ok compress/flate 1.025s
ok compress/gzip 0.099s
ok compress/lzw 0.018s
ok compress/zlib 0.099s
ok container/heap 0.020s
ok container/list 0.082s
ok container/ring 0.094s
ok context 1.127s
ok crypto 0.014s
ok crypto/aes 0.202s
ok crypto/cipher 0.045s
ok crypto/des 0.021s
ok crypto/dsa 0.015s
ok crypto/ecdsa 0.452s
ok crypto/elliptic 0.043s
ok crypto/hmac 0.021s
ok crypto/md5 0.060s
ok crypto/rand 0.142s
ok crypto/rc4 0.275s
ok crypto/rsa 0.123s
ok crypto/sha1 0.110s
ok crypto/sha256 0.018s
ok crypto/sha512 0.031s
ok crypto/subtle 0.015s
ok crypto/tls 1.354s
ok crypto/x509 2.934s
ok database/sql 0.592s
ok database/sql/driver 0.012s
ok debug/dwarf 0.032s
ok debug/elf 0.089s
ok debug/gosym 0.452s
ok debug/macho 0.066s
ok debug/pe 0.024s
ok debug/plan9obj 0.031s
ok encoding/ascii85 0.020s
ok encoding/asn1 0.025s
ok encoding/base32 0.017s
ok encoding/base64 0.017s
ok encoding/binary 0.014s
ok encoding/csv 0.016s
ok encoding/gob 0.098s
ok encoding/hex 0.016s
ok encoding/json 0.832s
ok encoding/pem 0.052s
ok encoding/xml 0.035s
ok errors 0.017s
ok expvar 0.062s
ok flag 0.013s
ok fmt 0.163s
ok go/ast 0.017s
ok go/build 0.986s
ok go/constant 0.015s
ok go/doc 0.123s
ok go/format 0.024s
ok go/importer 0.279s
ok go/internal/gccgoimporter 0.023s
ok go/internal/gcimporter 0.786s
ok go/internal/srcimporter 1.991s
ok go/parser 0.044s
ok go/printer 0.813s
ok go/scanner 0.018s
ok go/token 0.037s
ok go/types 1.625s
ok hash 0.070s
ok hash/adler32 0.023s
ok hash/crc32 0.023s
ok hash/crc64 0.029s
ok hash/fnv 0.023s
ok html 0.015s
ok html/template 0.111s
ok image 0.266s
ok image/color 0.039s
ok image/draw 0.078s
ok image/gif 0.526s
ok image/jpeg 0.755s
ok image/png 0.063s
ok index/suffixarray 0.022s
ok internal/cpu 0.019s
ok internal/poll 0.032s
ok internal/singleflight 0.025s
ok internal/trace 3.158s
ok io 0.040s
ok io/ioutil 0.024s
ok log 0.017s
ok log/syslog 1.292s
ok math 0.016s
ok math/big 3.591s
ok math/bits 0.017s
ok math/cmplx 0.016s
ok math/rand 0.643s
ok mime 0.020s
ok mime/multipart 0.859s
ok mime/quotedprintable 0.127s
ok net 3.517s
ok _/Users/didi/goLang/src/github.com/golang/go/test/bench/go1 9.896s
Go version is “go1.10”, ignoring -next /Users/didi/goLang/src/github.com/golang/go/api/next.txt
Installed Go for darwin/amd64 in /Users/didi/goLang/src/github.com/golang/go
Installed commands in /Users/didi/goLang/src/github.com/golang/go/bin
*** You need to add /Users/didi/goLang/src/github.com/golang/go/bin to your PATH.
/Users/didi/goLang/src/github.com/golang/go/bin/go version
go version go1.10 darwin/amd64
成功