Golang

Golang
Download and install
Documentation
A Tour of Go
Go by Example

command

init 初始化

1
go mod init example.com/greetings

run 运行不会生成二进制文件

1
2
3
go run hello.go
# or
go run .

test 在有单元测试的module中运行

1
2
3
go test
# -v flag to get verbose
go test -v

build 打包

1
2
# 在当前目录打包可执行二进制文件
go build

install 安装

1
2
# 安装二进制文件到 `~/go/bin/` 目录下
go install

dependency

配置国内加速镜像

查看全部配置

1
2
3
go env
# 查看部分配置
go env | grep GOPROXY

配置

1
2
3
4
5
6
# 官方
go env -w GOPROXY=https://goproxy.io,direct
# 七牛
go env -w GOPROXY=https://goproxy.cn,direct
# 阿里云
go env -w GOPROXY=https://mirrors.aliyun.com/goproxy/,direct

多个地址

1
2
3
4
# 逗号分割时,第一个URL return HTTP 404 or 410.将尝试第二个
go env -w GOPROXY=https://goproxy.cn,direct,https://mirrors.aliyun.com/goproxy
# 管道符号分割时,将尝试下一个URL,不管HTTP error code
go env -w GOPROXY=https://goproxy.cn,direct|https://mirrors.aliyun.com/goproxy

查看指定依赖

1
2
3
4
5
6
7
8
go list -m example.com/greetings
go list -m example.com/greetings@version
go list -m example.com/greetings@gitbranch
go list -m example.com/greetings@gitcommitcode
# 列出所有依赖模块信息,当前使用版本以及最新版本
go list -m -u all
# 列出指定依赖模块信息,当前使用版本以及最新版本
go list -m -u example.com/greetings

添加指定依赖

1
2
3
4
go get example.com/greetings
go get example.com/greetings@version
go get example.com/greetings@gitbranch
go get example.com/greetings@gitcommitcode

将依赖指向本地文件系统

1
2
go mod edit -replace=example.com/greetings=../greetings
go mod edit -replace=example.com/greetings@version=../greetings

整理项目

会移除未使用的 modules 会自动添加 go.mod 中构建包所需的 modules

1
go mod tidy

移除依赖 @none

1
go get example.com/theirmodule@none

清空模块缓存

1
2
3
# 模块缓存位置 `$GOPATH/pkg/mod` 查看 GOPATH
go env | grep GOPATH
go clean --modcache