Mac上安裝Go的一次記錄

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

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

安裝完成,驗證通過,大功告成。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.