国内下载
wget -c https://mirrors.ustc.edu.cn/golang/go1.12.9.linux-amd64.tar.gz
wget -c https://dl.google.com/go/go1.12.9.linux-amd64.tar.gz
wget -c https://gomirrors.org/dl/go/go1.12.9.linux-amd64.tar.gz
goproxy
export GOPROXY=https://mirrors.aliyun.com/goproxy
export GOPROXY=https://goproxy.io
export GOPRIVATE=*.corp.example.com
GO111MODULE=on CGO_ENABLED=0 go build
GO111MODULE=on CGO_ENABLED=0 go mod vendor
离线编译
# 开发过程
go mod vendor
GO111MODULE=on CGO_ENABLED=0 go build -mod=vendor
git add go.mod go.sum vendor
# 编译
test -d /usr/local/go/bin && export GOROOT=/usr/local/go && export PATH=$PATH:$GOROOT/bin
test -f go.mod && GO111MODULE=on go build -mod=vendor
golang 1.10 报错的解决办法:
go tool asm: exit status 2
flag provided but not defined: -V
runtime/internal/sys
flag provided but not defined: -std
解决: ubuntu 系统:
export GOROOT=/usr/lib/go
CentOS 系统:
export GOROOT=/usr/lib/golang
不同版本覆盖混用会导致如下报错:
解决办法:删除部署路径的golang,全新部署
# crypto/rc4
/usr/local/go/src/crypto/rc4/rc4_asm.go:15:18: (*Cipher).XORKeyStream redeclared in this block
previous declaration at /usr/local/go/src/crypto/rc4/rc4.go:61:6
# strings
/usr/local/go/src/strings/strings_decl.go:8:6: IndexByte redeclared in this block
previous declaration at /usr/local/go/src/strings/strings.go:150:34
# 或者如下报错:
+ go build -mod=vendor
cannot load crypto/x509/pkix: open /app/wwwroot/advertApi/vendor/crypto/x509/pkix: no such file or directory
snippet
- int64 to String
import "strconv"
strconv.FormatInt(int64Value, 10)
- string to int64
import "strconv"
int64Value, err := strconv.ParseInt(strValue, 10, 64)
if err != nil {
log.Fatal(err)
}
依赖管理,推荐 govendor
go get github.com/kardianos/govendor
go get -u github.com/tools/godep
brew install glide or curl https://glide.sh/get | sh
vscode cmd tools
go get -v github.com/golang/lint/golint
go get -v github.com/tpng/gopkgs
go get -v github.com/lukehoban/go-outline
go get -v github.com/nsf/gocode
go get -v sourcegraph.com/sqs/goreturns
go get -v github.com/rogpeppe/godef
cmd tools
go get github.com/joho/godotenv/cmd/godotenv dotenv
将静态文件打包到二进制中
go get -u -v github.com/jteeuwen/go-bindata/...
go get github.com/GeertJohan/go.rice/...
web
go get -u gopkg.in/gin-gonic/gin.v1
go get -u gopkg.in/macaron.v1
go get -u github.com/labstack/echo/...
lib
https://github.com/joho/godotenv
gprc
brew install protobuf protobuf-c
go get -u google.golang.org/grpc
go get -u github.com/golang/protobuf/{proto,protoc-gen-go}
go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway
go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger
json to struct
https://mholt.github.io/json-to-go/
优秀库
日志
https://github.com/golang/glog
https://github.com/sirupsen/logrus
错误处理 https://github.com/juju/errors
配置读取 https://github.com/spf13/viper
测试框架 https://onsi.github.io/ginkgo/
go get github.com/onsi/gomega
高效json库 github.com/json-iterator/go
实用工具
依赖库改名 go get -u golang.org/x/tools/cmd/gomvpkg
client lib for DB/NOSQL/Kafka
github.com/jinzhu/gorm
github.com/go-xorm/xorm
github.com/go-sql-driver/mysql
github.com/garyburd/redigo/redis
gopkg.in/mgo.v2
gopkg.in/ldap.v2
github.com/go-xorm/xorm
github.com/bradfitz/gomemcache/memcache
go get gopkg.in/Shopify/sarama.v1 # kafka github.com/Shopify/sarama
github.com/smallnest/goreq # HTTP client lib
links
https://emacsist.github.io/2016/10/18/Go%E5%AD%A6%E4%B9%A0/
http://colobu.com/2015/09/07/gotchas-and-common-mistakes-in-go-golang/
http://colobu.com/2017/02/01/golang-summaries/
https://go-zh.org/doc/effective_go.html
//www.greatytc.com/p/f30da01eea97
https://medium.com/@_orcaman/most-imported-golang-packages-some-insights-fb12915a07