1. # 创建项目目录
  2. mkdir module-name
  3. cd module-name
  4. # 初始化module
  5. go mod init module-name
  6. # 查看生成的mod
  7. cat go.mod
  8. # 查看项目结构和依赖
  9. tree -L 1 .
  10. # 第一次运行会自动检查代码里的第三方依赖包,并下载项目依赖的包
  11. go run main.go
  12. # 执行go help mod可以查看到以下子命令
  13. go help mod
  14. download download modules to local cache # 下载模块包到本地缓存
  15. edit edit go.mod from tools or scripts # 从工具或脚本中修改go.md内容(暂时没觉得很好用)
  16. graph print module requirement graph # 打印模块依赖树
  17. init initialize new module in current directory # 在当前目录初始化一个模块
  18. tidy add missing and remove unused modules # 添加或略或删除不使用的包
  19. vendor make vendored copy of dependencies # 将依赖拷贝到vendor中(会将依赖打包到当前目录下的vendor目录)
  20. verify verify dependencies have expected content # 检查依赖内容
  21. why explain why packages or modules are needed # 解释为什么哪些包或者模块被需要
  1. $ go get -u google.golang.org/grpc
  2. package google.golang.org/grpc: unrecognized import path "google.golang.org/grpc" (https fetch: Get https://google.golang.org/grpc?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
  3. $ git clone https://github.com/grpc/grpc-go.git $GOPATH/src/google.golang.org/grpc
  4. go mod edit -replace=google.golang.org/grpc=github.com/grpc/grpc-go@latest
  5. go mod tidy
  6. go mod vendor
  7. go build -mod=vendor
  1. module <your module name>
  2. require (
  3. google.golang.org/grpc v1.27.0
  4. )
  1. go mod edit -require=github.com/LonelyPale/goutils@master
  2. go mod edit -require=golang.org/x/net@master
  3. go mod edit -require=golang.org/x/net@latest

参考:

Modules

跳出Go module的泥潭

Go module 再回顾

在go modules里使用go get进行包管理

Go 1.11 modules的填坑之旅

用 golang 1.11 module 做项目版本管理

Go Module 简明教程