Skip to content

Latest commit

 

History

History
146 lines (132 loc) · 4.7 KB

0.md

File metadata and controls

146 lines (132 loc) · 4.7 KB

环境设置

如何设置go环境变量GOPATH

如何设置go环境变量GOPATH?
安装gin-gonic/gin 报错: cannot download, $GOPATH not set. For more details see: go help gopath

$ go get -u github.com/gin-gonic/gin
package github.com/gin-gonic/gin:
cannot download, $GOPATH not set.
For more details see: go help gopath

直接在终端输入echo $GOPATH,没有任何返回值。

设置 GOPATH 环境变量
创建 GOPATH 文件夹:

cd
mkdir go
  • 临时设置 GOPATH 环境变量:
    mkdir $HOME/go
    export GOPATH=$HOME/go
  • 开机自动加载 GOPATH 环境变量:
    • 如果是Mac则需要在~/.bash_profile或者~/.zshrc中添加:
      # GOPATH
      export GOPATH=$HOME/go
      然后使其生效:
      source ~/.bash_profile # source ~/.zshrc
    • 如果是 Ubuntu, 可能需要在 ~/.bashrc 或者~/.zshrc中添加:
      # GOPATH
      export GOPATH=$HOME/go
      然后使其生效:
      source ~/.bashrc # source ~/.zshrc

再次查看 $GOPATH 环境变量:

# Mac
echo $GOPATH
/Users/andy/go

# Ubuntu
echo $GOPATH
/home/andy/go/

参考链接:

https://github.com/golang/go/wiki/SettingGOPATH


go获取tools包

go 获取golang.org/x/tools包,官方推荐的最简单的方法是:

go get -u golang.org/x/tools/...

由于国内网络链接不上Google的原因,设置代理也不成功,提示如下:

get -v -u golang.org/x/tools/...
Fetching https://golang.org/x/tools/benchmark/parse?go-get=1
https fetch failed: Get https://golang.org/x/tools/benchmark/parse?go-get=1: dial tcp 216.239.37.1:443: i/o timeout
golang.org/x/tools (download)
Fetching https://golang.org/x/tools/blog?go-get=1
https fetch failed: Get https://golang.org/x/tools/blog?go-get=1: dial tcp 216.239.37.1:443: i/o timeout
Fetching https://golang.org/x/tools/blog/atom?go-get=1
https fetch failed: Get https://golang.org/x/tools/blog/atom?go-get=1: dial tcp 216.239.37.1:443: i/o timeout
Fetching https://golang.org/x/tools/present?go-get=1
https fetch failed: Get https://golang.org/x/tools/present?go-get=1: dial tcp 216.239.37.1:443: i/o timeout

替代的解决办法就是直接去https://github.com/golang/tools.git 克隆一份然后放到$GOPATH/src/golang.org/x/tools目录下($GOPATHgo path路径)

cd $GOPATH/src/golang.org/x/
git clone https://github.com/golang/tools.git

/src/golang.org/x/不存在则:

cd $HOME/go
mkdir -p src/golang.org/x/
cd $GOPATH/src/golang.org/x/
git clone https://github.com/golang/tools.git

手动安装非预装扩展包:

golang 官方在 github 上建立了一个镜像库,直接从github下载镜像代码,放入GOPATH目录下即可。 直接通过 go get 安装,报错:

go get golang.org/x/tour
package golang.org/x/tour:
unrecognized import path "golang.org/x/tour"
(https fetch: Get https://golang.org/x/tour?go-get=1:dial tcp 216.239.37.1:443: i/o timeout)

go get -u golang.org/x/blog
package golang.org/x/blog:
unrecognized import path "golang.org/x/blog"
(https fetch: Get https://golang.org/x/blog?go-get=1:dial tcp 216.239.37.1:443: i/o timeout)

拉取各个模块的代码, 比如拉取 x/net 代码,直接执行:

git clone https://github.com/golang/net.git

可以将Go的各个子包都给下载下来:

git clone https://github.com/golang/tour.git
git clone https://github.com/golang/net.git
git clone https://github.com/golang/blog.git
git clone https://github.com/golang/sync.git
git clone https://github.com/golang/tools.git
git clone https://github.com/golang/lint.git
# ...

每个包的名称如下:

benchmarks — benchmarks to measure Go as it is developed.
blog — blog.golang.org's implementation.
build — build.golang.org's implementation.
crypto — additional cryptography packages.
debug — an experimental debugger for Go.
image — additional imaging packages.
mobile — experimental support for Go on mobile platforms.
net — additional networking packages.
perf — packages and tools for performance measurement, storage, and analysis.
review — a tool for working with Gerrit code reviews.
sync — additional concurrency primitives.
sys — packages for making system calls.
text — packages for working with text.
time — additional time packages.
tools — godoc, goimports, gorename, and other tools.
tour — tour.golang.org's implementation.
exp — experimental and deprecated packages (may change without warning)