1,安装
brew install go
2,设置环境变量:
GOPATH 是运行程序的目录
GOROOT 是安装目录,
export GOPATH=/Users/songfei/sdk/go
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOBIN
3,
$~go env
GOARCH=”amd64”
GOBIN=”/Users/songfei/sdk/go/bin”
GOEXE=””
GOHOSTARCH=”amd64”
GOHOSTOS=”darwin”
GOOS=”darwin”
GOPATH=”/Users/songfei/sdk/go”
GORACE=””
GOROOT=”/usr/local/Cellar/go/1.5.2/libexec”
GOTOOLDIR=”/usr/local/Cellar/go/1.5.2/libexec/pkg/tool/darwin_amd64”
GO15VENDOREXPERIMENT=””
CC=”clang”
GOGCCFLAGS=”-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fno-common”
CXX=”clang++”
CGO_ENABLED=”1”
hello.go
package main
import "fmt"
func main() {
fmt.Printf("hello, world\n")
}
4, 编译成可执行文件
go build hello.go
# native compiler drawin amd64
GOROOT=/usr/local/Cellar/go/1.5.2/libexec
GOBIN=/Users/songfei/sdk/go/bin
GOARCH=amd64
GOOS=darwin
CGO_ENABLED=1
PATH=$PATH:$GOBIN
GO15VENDOREXPERIMENT=1
PATH=$GOROOT/bin:$PATH
LITEIDE_GDB=/usr/local/bin/gdb
LITEIDE_MAKE=make
LITEIDE_TERM=/usr/bin/open
LITEIDE_TERMARGS=-a Terminal
LITEIDE_EXEC=/usr/X11R6/bin/xterm
LITEIDE_EXECOPT=-e
如果运行过程中报错:
GolangDoc: Could not find godoc, (hint: is godoc installed?)
解决办法:
翻墙
go get -v -u golang.org/x/tools/cmd/godoc
不翻墙
git clone https://github.com/golang/tools $GOPATH/src/golang.org/x/tools
有疑问加站长微信联系(非本文作者)