Go语言环境配置

标签: go, 环境配置, mac

0x01 brew大法好

Brew大法好,用过都说好,它好我也好。

安装brew

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
//安装成功后更新
brew update

安装Go

brew install go
==> Downloading https://homebrew.bintray.com/bottles/go-1.5.1.yosemite.bottle.ta
######################################################################## 100.0%
==> Pouring go-1.5.1.yosemite.bottle.tar.gz
==> 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
...

0x02 配置Go环境

按照提示设置PATH变量。首先要明确使用的什么shell,例如:

~  echo $SHELL
/bin/zsh

然后按照Go安装后的提示,去修改对应的bash配置文件。修改PATH变量,增加GOPATH变量
zsh对应的是 ~/.zshrc

export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/opt/go/libexec/bin"
export GOPATH="/Users/yourname/go/src/github.com:/Users/yourname/go/src/work"

检查环境配置

~  go env
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/yourname/go/src/github.com:/Users/yourname/go/src/work"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.5.1/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.5.1/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"

这里GOPATH为多路径配置,不同的Project源码放在不同的目录。

0x03 Go:Hello Moses!

/Users/yourname/go/src/work项目文件夹下创建main.go

package main

import "fmt"

func main() {
    fmt.Println("Hello Moses!");
}

然后执行go install

生成的程序在/Users/yourname/go/bin中,执行:

bin  ./work
Hello Moses!

欢迎来到Go的世界!enjoy it.


评论已关闭