這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
0、準備工作
現在是2017年6月底,我用MacBook Air試著源碼安裝Go,參考文檔是 https://golang.org/doc/install/source。
1、下載源碼
準備好一個乾淨的目錄,檢出原始碼。官方文檔步驟如下:
$ git clone https://go.googlesource.com/go$ cd go$ git checkout go1.8.3
我執行時,檢出分支時出錯,遠程不存在go1.8.3這個分支。
$ git branch* master$ git checkout go1.8.3Note: checking out 'go1.8.3'.You are in 'detached HEAD' state. You can look around, make experimentalchanges and commit them, and you can discard any commits you make in thisstate without impacting any branches by performing another checkout.If you want to create a new branch to retain commits you create, you maydo so (now or later) by using -b with the checkout command again. Example: git checkout -b <new-branch-name>HEAD is now at 352996a381... [release-branch.go1.8] go1.8.3$ git branch* (HEAD detached at go1.8.3) master$ git branch -a* (HEAD detached at go1.8.3) master remotes/origin/HEAD -> origin/master remotes/origin/dev.cc remotes/origin/dev.garbage remotes/origin/dev.gcfe remotes/origin/dev.inline remotes/origin/dev.power64 remotes/origin/dev.ssa remotes/origin/dev.tls remotes/origin/dev.typealias remotes/origin/master remotes/origin/release-branch.go1 remotes/origin/release-branch.go1.1 remotes/origin/release-branch.go1.2 remotes/origin/release-branch.go1.3 remotes/origin/release-branch.go1.4 remotes/origin/release-branch.go1.5 remotes/origin/release-branch.go1.6 remotes/origin/release-branch.go1.7 remotes/origin/release-branch.go1.8 remotes/origin/release-branch.r57 remotes/origin/release-branch.r58 remotes/origin/release-branch.r59 remotes/origin/release-branch.r60
事實上go1.8.3是個tag,不是branch:
$ git tag -l | grep 1.8go1.8go1.8.1go1.8.2go1.8.3go1.8beta1go1.8beta2go1.8rc1go1.8rc2go1.8rc3
從tag檢出:
$ git checkout -b go1.8.3 go1.8.3Switched to a new branch 'go1.8.3'$ git branch* go1.8.3 master$ lsAUTHORS CONTRIBUTORS PATENTS VERSION doc lib robots.txt testCONTRIBUTING.md LICENSE README.md api favicon.ico misc src$ cat VERSION go1.8.3
檢出1.8.3成功,繼續執行後續安裝步驟。
2、安裝
官方文檔步驟只需要兩步:
$ cd src$ ./all.bash
我執行後報錯,缺少Go bootstrap tool(忘記了Go已經完成了自舉,需要安裝Go1.4編譯器了。):
$ ./all.bash ##### Building Go bootstrap tool.cmd/distERROR: Cannot find /Users/xingyongtao/go1.4/bin/go.Set $GOROOT_BOOTSTRAP to a working Go tree >= Go 1.4.
關於這個問題,官方文檔是這麼說的:
To build a bootstrap tool chain from source, use either the git branch release-branch.go1.4 or go1.4-bootstrap-20170531.tar.gz, which contains the Go 1.4 source code plus accumulated fixes to keep the tools running on newer operating systems. (Go 1.4 was the last distribution in which the tool chain was written in C.) After unpacking the Go 1.4 source, cd to the src subdirectory and run make.bash (or, on Windows, make.bat).
所以我只需要在本地把go1.4分支檢出來,安裝就行。
$ git checkout release-branch.go1.4Branch release-branch.go1.4 set up to track remote branch release-branch.go1.4 from origin.Switched to a new branch 'release-branch.go1.4'$ git branch go1.8.3 master* release-branch.go1.4
安裝比較順利,安裝完設定環境變數GOROOT_BOOTSTRAP。
(期間因報錯拷貝過一份go目錄,使go1.4和GOROOT不為同一個目錄。)
重新執行./all.bash安裝1.8.3,報重複聲明的錯誤,是切換branch重複編譯導致的。清理後重試即可:
git clean -xf
安裝完成,驗證通過,大功告成。