建立開發除錯環境
- 建立開發環境
- 安裝輸入法
- 更新軟體源
- 安裝gcc
- Go環境編譯
- C版本Go1.4
- Go版本Go1.5
建立開發環境
為了對Golang有一個更深入的學習,搭建一個可以開發除錯Golang源代碼的環境是必要的,此處我們選定Ubuntu作業系統作為除錯環境,
安裝Ubuntu16.04.1,這個步驟比較簡單一般不會出錯,
安裝輸入法
安裝玩Ubuntu后機器默認不支持中文輸入法,需要先安裝中文輸入法,具體安裝程序參考Ubuntu 16.04搜狗輸入法安裝
更新軟體源
系統默認源速度太慢,可以更新為國內的源,此處使用阿里云源,具體方法如下:
sudo vi /etc/apt/sources.list
# 阿里云deb cdrom:[Ubuntu 16.04 LTS _Xenial Xerus_ - Release amd64 (20160420.1)]/ xenial main restricted
deb-src http://archive.ubuntu.com/ubuntu xenial main restricted #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates universe
deb http://mirrors.aliyun.com/ubuntu/ xenial multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial-security universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-security multiverse
最后
sudo apt update
sudo apt upgrade
系統默認的vi不太好用升級一下vim
sudo apt install vim
安裝gcc
系統默認安裝的gcc版本比較新為gcc 7,go1.4版本代碼有編譯告警因此報錯,因此此處安裝稍微舊一些的版本可以避免這個問題,此處我安裝的是gcc-5
安裝gcc命令:sudo apt-get install -y gcc-5
Go環境編譯
GO1.5實自舉(用GO實作GO的編譯器),因此保留兩個版本的Go環境,
- Go1.4是用C語言實作的最后一個版本,可以用這個版本作為編譯器編譯代碼Go1.5,
- GO1.5是第一個Go語言實作自舉的版本,因此可以先嘗試分析這個版本的代碼,
C版本Go1.4
在https://studygolang.com/dl下載源代碼包go1.4-bootstrap-20171003.tar.gz ,解壓后進入src目錄運行
sudo CGO_ENABLED=0 ./make.bash
最后回到bin目錄下,運行一下bin目錄下的go程式如果如下圖所示就編譯成功了,

注意CGO_ENABLED=0不可以省略,否則可能會報錯,
Go版本Go1.5
編譯Go1.5和編譯Go1.4類似,將下載的Go1.5代碼包解壓后進入src目錄,運行以下命令
sudo CGO_ENABLED=0 GOROOT_BOOTSTRAP=/home/zzl/golang/go1.4 ./make.bash
注意以上命令不可以分成兩條,否則會報下圖的錯誤,

如果省略了CGO_ENABLED=0有可能報下面的錯誤,
/home/zzl/Downloads/go/pkg/linux_amd64/runtime/cgo.a(_all.o): unknown relocation type 42; compiled without -fpic?
/home/zzl/Downloads/go/pkg/linux_amd64/runtime/cgo.a(_all.o): unknown relocation type 42; compiled without -fpic?
/home/zzl/Downloads/go/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
runtime/cgo(.text): unexpected relocation type 298
# cmd/pprof
/home/zzl/Downloads/go/pkg/linux_amd64/runtime/cgo.a(_all.o): unknown relocation type 42; compiled without -fpic?
/home/zzl/Downloads/go/pkg/linux_amd64/runtime/cgo.a(_all.o): unknown relocation type 42; compiled without -fpic?
/home/zzl/Downloads/go/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
runtime/cgo(.text): unexpected relocation type 298
最后回到go1.5目錄下的bin子目錄運行一下go命令檢查編譯是否成功:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/238603.html
標籤:區塊鏈
