Golang go mod 包管理

文章目录

    • 使用开源发布的包
    • 使用本地包
      • demo
      • study
    • 使用仓库包
      • demo
      • study
    • vendor

使用开源发布的包

go get 安装依赖即可
例如:
go get -u github.com/gin-gonic/gin

使用本地包

只需要在 go.mod 中 通过 replace 将包地址更改为本地地址即可,例如
replace demo => /Users/xieruixiang/go/src/demo
下面通过 study项目使用demo项目的代码来为大家做一个演示

demo

  • 先通过 go mod init demo 初始化
  • 然后增加代码即可

Golang go mod 包管理_第1张图片
image.png

study

image.png

在go.mod 中增加 replace 替换,让其去本地路径找 :
replace demo => /Users/xieruixiang/go/src/demo
Golang go mod 包管理_第2张图片

通过 go get 获取 :
get get demo
image.png
完成上述操作就能在 study 中 使用 本地 demo 项目中的代码了
Golang go mod 包管理_第3张图片

使用仓库包

demo

要先将demo项目提交到 gitee中,提交之前要将 mod 名称 改为 gitee项目地址的名称
go mod init gitee.com/cdet/demo

study

study 中要设置 go 环境变量 GOPRIVATE = 仓库域名
GOPRIVATE = gitee.com
Golang go mod 包管理_第4张图片
在 study 中 通过 go get 来安装
image.png
然后就能够使用了
Golang go mod 包管理_第5张图片

vendor

go mod vendor 会将你的依赖包 copy 到项目根目录下的 vendor 中,使用 vendor中的包,你可以在vendor中对源码进行调试,在 vendor 模式下,go build 构建会使用 vendor 中的包,如果想要退出 vendor 模式,直接删除 vendor 目录即可
Golang go mod 包管理_第6张图片

你可能感兴趣的:(Golang,#,go从入门到精通,golang,开发语言,后端)