背景
因为各类问题,国内使用 go get 安装 golang 官方包可能会失败,如我本身在安装 collidermain 时,出现了如下报错:git
$ go get collidermain package golang.org/x/net/websocket: unrecognized import path "golang.org/x/net/websocket" (https fetch: Get https://golang.org/x/net/websocket?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
原理
https://github.com/golang/nethttps://golang.org/x/net
获取 golang.org/x/net 包,其实只须要如下步骤:golang
mkdir -p $GOPATH/src/golang.org/x cd $GOPATH/src/golang.org/x git clone https://github.com/golang/net.git
其它 golang.org/x 下的包获取皆可以使用该方法。web
因此在这里写了一个bash脚本一键安装golang.org/x的包
chmod +x gox.sh./gox.sh
#!/usr/bin/env bash #为了兼容windows下的bash cd $GOPATH; #建立 $GOPATH/src/golang.org/x 目录 mkdir -p $GOPATH/src/golang.org/x; echo "已经安装的golang.org/x package"; ls; echo "下面一行 for in 中包的名字您能够本身来定义" for name in "glog" "image" "perf" "snappy" "term" "sync" "winstrap" "cwg" "leveldb" "text" "net" "build" "protobuf" "dep" "sys" "crypto" "gddo" "tools" "scratch" "proposal" "mock" "oauth2" "freetype" "debug" "mobile" "gofrontend" "lint" "appengine" "geo" "review" "arch" "vgo" "exp" "time";do cd $GOPATH/src/golang.org/x; if [ -d "$name" ] then cd $name; echo $name "包已经存在,使用git pull来更新源码"; git pull; else git_url="https://github.com/golang/${name}.git"; echo "开始clone golang.org/x 在github.com上的镜像代码:${git_url}"; git clone --depth 1 "$git_url"; cd $name; fi #go install; done
注意
在运行脚本的过程,应为git clone 太频繁,须要输入你的GitHub帐号shell