Mac上安装Go的一次记录

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.3
Note: checking out 'go1.8.3'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b 

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.8
go1.8
go1.8.1
go1.8.2
go1.8.3
go1.8beta1
go1.8beta2
go1.8rc1
go1.8rc2
go1.8rc3

从tag检出:

$ git checkout -b go1.8.3 go1.8.3
Switched to a new branch 'go1.8.3'
$ git branch
* go1.8.3
  master
$ ls
AUTHORS     CONTRIBUTORS    PATENTS     VERSION     doc     lib     robots.txt  test
CONTRIBUTING.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/dist
ERROR: 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.4
Branch 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

安装完成,验证通过,大功告成。

你可能感兴趣的:(Mac上安装Go的一次记录)