使用 brew
我使用brew 安装它。
$ brew install go
当你运行这个 brew 命令完成后,它会显示以下信息:
$ brew info go
go: stable 1.4.2 (bottled), HEAD
Go programming environment
https://golang.org
/usr/local/Cellar/go/1.4.2 (4676 files, 158M) *
Poured from bottle
From: https://github.com/Homebrew/homebrew/blob/master/Library/Formula/go.rb
==> Options
--with-cc-all
Build with cross-compilers and runtime support for all supported platforms
--with-cc-common
Build with cross-compilers and runtime support for darwin, linux and windows
--without-cgo
Build without cgo
--without-godoc
godoc will not be installed for you
--without-vet
vet will not be installed for you
--HEAD
Install HEAD version
==> Caveats
As of go 1.2, a valid GOPATH is required to use the `go get` command:
https://golang.org/doc/code.html#GOPATH
You may wish to add the GOROOT-based install location to your PATH:
export PATH=$PATH:/usr/local/opt/go/libexec/bin
其中重要的部分有以下几行:
/usr/local/Cellar/go/1.4.2(4676 个文件,158M)*
导出 PATH=$PATH:/usr/local/opt/go/libexec/bin
设置 GO 的环境
这显示了 GO 的安装位置。我们需要做以下设置GO的环境:
$ export PATH=$PATH:/usr/local/opt/go/libexec/bin
$ export GOPATH=/usr/local/opt/go/bin
然后您可以使用 GO 检查它是否配置正确:
$ go env
GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/usr/local/opt/go/bin"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.4.2/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.4.2/libexec/pkg/tool/darwin_amd64"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fno-common"
CXX="clang++"
CGO_ENABLED="1"
设置 json2csv
看起来不错,让我们安装json2csv:
$ go get github.com/jehiah/json2csv
$
刚刚发生了什么?它安装了它。你可以这样检查:
$ $ ls -l $GOPATH/bin
total 5248
-rwxr-xr-x 1 sammingolelli staff 2686320 Jun 9 12:28 json2csv
好的,那为什么我不能在我的 shell 中输入 json2csv 呢?那是因为$GOPATH 下的/bin 目录不在您的$PATH 上。
$ type -f json2csv
-bash: type: json2csv: not found
所以让我们暂时添加它:
$ export PATH=$GOPATH/bin:$PATH
并重新检查:
$ type -f json2csv
json2csv is hashed (/usr/local/opt/go/bin/bin/json2csv)
现在它在那里:
$ json2csv --help
Usage of json2csv:
-d=",": delimiter used for output values
-i="": /path/to/input.json (optional; default is stdin)
-k=[]: fields to output
-o="": /path/to/output.json (optional; default is stdout)
-p=false: prints header to output
-v=false: verbose output (to stderr)
-version=false: print version string
将我们对$PATH 和$GOPATH 所做的修改添加到您的$HOME/.bash_profile,以使它们在重新启动之间保持不变。