0%

go 百宝箱

go mod 的使用

1
2
3
4
5
6
7
8
# xxx表示项目的根域名
go mod init "xxx"
# 映射
go mod edit -replace=golang.org/x/sys@v0.0.0=github.com/golang/sys@v0.0.0
# 执行以下命令会自动分析项目里的依赖关系同步到go.mod文件中,同时创建go.sum文件
go mod tidy
# 直接使用这个命令就可以把GOPATH/src目录下的依赖包同步到当前项目目录中
go mod vendor

设置国内源

1
2
export GOPROXY=https://goproxy.io
export GOPROXY=https://mirrors.aliyun.com/goproxy/

go 可能需要用到的环境变量

1
2
3
4
5
6
export GOROOT=/usr/local/go
export GOARCH=amd64
export GOPATH=/usr/local/gopath
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOPATH/bin
export GOBIN=/home/linuxbrew/.linuxbrew/bin/

开启或关闭go mod

1
2
3
4
export GO111MODULE=auto
#auto 自动
#on 使用mod
#off 不使用mod

go类型转换

转载:https://www.jb51.net/article/119164.htm

int转string

1
2
s := strconv.Itoa(i)
s := strconv.FormatInt(int64(i), 10)

int64转string

1
2
i := int64(123)
s := strconv.FormatInt(i, 10)

string转int

1
i, err := strconv.Atoi(s))

string转int64

1
i, err := strconv.ParseInt(s, 10, 64)

go 字符串操作

转载:https://blog.csdn.net/li_101357/article/details/80241224

1
2
3
4
5
6
# 子串substr在s中,返回true
func Contains(s, substr string) bool
# chars中任何一个Unicode代码点在s中,返回true
func ContainsAny(s, chars string) bool
# Unicode代码点rs中,返回true
func ContainsRune(s string, r rune) bool

电子书

《Effective Go》中英双语版