之前迷上了神奇的Go语言,就在windows上装上了Go1.8,挺顺风顺水的,不过最近换上了Linux时,编译安装就炸了,各种报错。今天就留个记录,备个份!

编译安装Go1.8的主体思路:

  1. github中clone下来
  2. 确保linux系统安装较新的libc,gcc(apt,yum随便搞上)
  3. 切换库分支至1.4(刚clone下来的库),去到src目录,执行all.bash
  4. 把编译安装完的1.4目录cp到/root/,并更名为go1.4
  5. 将刚刚的编译安装的仓库恢复原状(git clean -dfx),在切换到1.8,同样刚刚的操作,执行all.bash,安装1.8
  6. 然后1.8安装完后,配置profile

坑点

1.由于Go1.8的安装需要go1.4的二进制,所以,如果没安装go1.4的小伙伴,直接安装1.8估计会这样

1
Set $GOROOT_BOOTSTRAP to a working Go tree >= Go 1.4.

这个GOROOT_BOOTSTRAP地址默认在/root/go1.4(~/go1.4)

2.用all.bash安装1.4时炸了

报错如下

1
2
3
4
5
# cmd/pprof
.../go1.4/pkg/linux_amd64/runtime/cgo.a(_all.o): unknown relocation type 42; compiled without -fpic?
.../go1.4/pkg/linux_amd64/runtime/cgo.a(_all.o): unknown relocation type 42; compiled without -fpic?
runtime/cgo(.text): unexpected relocation type 298
runtime/cgo(.text): unexpected relocation type 298

拿去不用谢我.jpg

所以解决方案是

1
2
$ cd ~/go1.4/src
$ CGO_ENABLED=0 ./make.bash

导致的原因是:CGO_ENABLED: Controls cgo usage during the build. Set it to 1 to include all cgo related files, .c and .go file with “cgo” build directive, in the build. Set it to 0 to ignore them.在构建过程中控制cgo的使用。当设置为1,在构建时,会包含所有cgo相关的文件,如带有”cgo”编译指令的.c和.go文件。当设置为0,则忽略它们(即禁用CGO)

3.在顺利的安装完1.4后再安装1.8,应该也没问题了,最后设置一下环境变量

1
vim /ect/profile

在里面加上:

1
2
3
# Golang Env
export PATH=$PATH:/home/xx/go/bin //xx根据实际情况填写
export GOPATH=/home/xx/go-projects //xx根据实际情况填写

然后重启电脑,执行

1
go version

显示到版本号的话就可以愉快的玩耍了!

总结

虽然安装的历程有点坎坷,不过看在go编程这么奇葩有趣的份上,忍了吧!O(∩_∩)O哈哈~

参考

中国网络环境下源码编译安装Go1.6