Go -- mac 配置Go环境

mac 配置GO环境

1、安装go

brew install go

2、查看go的安装环境

go env

3、配置环境变量

编辑 ~/.bash_profile 文件,添加如下几行代码:

GOROOT=/usr/local/Cellar/go/1.10.1/libexec
export GOROOT
export GOPATH=/Users/jim/mygo
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOBIN:$GOROOT/bin

{

  • GOROOT: go安装目录
  • GOPATH:go工作目录,作为编译后二进制的存放目的地和import包时的搜索路径。其实说通俗点就是你的go项目工作目录。通常情况下GOPATH包含三个目录:bin、pkg、src。
  • GOBIN:go可执行文件目录
  • PATH:将go可执行文件加入PATH中,使GO命令与我们编写的GO应用可以全局调用

src目录下主要存放go的源文件

pkg目录存放编译好的库文件,主要是*.a文件;

bin目录主要存放可执行文件

}

编辑完之后执行:

source ~/.bash_profile

4、安装包管理工具glide (具体可参考网址:https://blog.csdn.net/to_be_better/article/details/78149713)

brew install glide

5、执行glide安装依赖包

glide install

遇到的问题:

[WARN]	Unable to checkout golang.org/x/crypto
[ERROR]	Update failed for golang.org/x/crypto: Cannot detect VCS
[WARN]	Unable to checkout golang.org/x/time
[ERROR]	Update failed for golang.org/x/time: Cannot detect VCS
[WARN]	Unable to checkout golang.org/x/net
[ERROR]	Update failed for golang.org/x/net: Cannot detect VCS
[WARN]	Unable to checkout golang.org/x/sys/unix
[WARN]	Unable to checkout google.golang.org/grpc
[ERROR]	Update failed for google.golang.org/grpc: Cannot detect VCS
[ERROR]	Update failed for golang.org/x/sys/unix: Cannot detect VCS
[ERROR]	Failed to install: Cannot detect VCS

因为golang.org被墙的原因,所以国内安装其中的库会失败(Cannot detect VCS)。

$ rm -rf ~/.glide
$ mkdir -p ~/.glide
$ glide mirror set https://golang.org/x/mobile https://github.com/golang/mobile --vcs git
$ glide mirror set https://golang.org/x/crypto https://github.com/golang/crypto --vcs git
$ glide mirror set https://golang.org/x/net https://github.com/golang/net --vcs git
$ glide mirror set https://golang.org/x/tools https://github.com/golang/tools --vcs git
$ glide mirror set https://golang.org/x/text https://github.com/golang/text --vcs git
$ glide mirror set https://golang.org/x/image https://github.com/golang/image --vcs git
$ glide mirror set https://golang.org/x/sys https://github.com/golang/sys --vcs git

添加之后,运行glide init 还是报下面的错:

[WARN]	Unable to checkout golang.org/x/sys/unix
[ERROR]	Update failed for golang.org/x/sys/unix: Cannot detect VCS
[WARN]	Unable to checkout golang.org/x/time
[ERROR]	Update failed for golang.org/x/time: Cannot detect VCS
[WARN]	Unable to checkout google.golang.org/grpc
[ERROR]	Update failed for google.golang.org/grpc: Cannot detect VCS
[ERROR]	Failed to install: Cannot detect VCS

可以看出来,镜像文件没有添加完全,因此再加入如下的镜像设置:

glide mirror set https://golang.org/x/time https://github.com/golang/time --vcs git
glide mirror set https://golang.org/x/sys/unix https://github.com/golang/sys/unix --vcs git
glide mirror set https://golang.org/grpc https://github.com/golang/grpc --vcs git

还是报错,怀疑是上面的镜像设置错误

[ERROR]	Update failed for google.golang.org/grpc: Cannot detect VCS  # 未找到,待解决
fatal: repository 'https://github.com/golang/unix/' not found   # 未找到,待解决

改变镜像路径

glide mirror set https://golang.org/x/sys/unix https://github.com/golang/sys --vcs git
glide mirror set https://google.golang.org/grpc https://github.com/grpc/grpc-go --vcs git

6、运行项目

1)把项目放到GOPATH 目录下面,参考环境变量的配置,export GOPATH=/Users/jim/mygo,也就是放到~/mygo/src/bitbucket.org/目录下

2)然后在项目中执行

$ glide init

$ glide install

如果执行:glide init,出现如下错误:

[ERROR]	Cowardly refusing to overwrite existing YAML

删除项目目录下的 glide.yaml
跳过这一步,执行glide install

如果某些包实在找不到,就单独安装某个包
举例如下:
go get github.com/golang/grpc

你可能感兴趣的:(GO语言)