当前位置:网站首页>golang项目依赖管理工具go vendor,go mod
golang项目依赖管理工具go vendor,go mod
2022-06-25 13:29:00 【Leo Han】
我们在进行golang项目编程的时候,一般会面临比较复杂的golang的依赖问题,可以通过go vendor或者go mod来解决
如果go get出错,在命令行进行如下设置:
go env -w GOPROXY=https://goproxy.cn,direct
首先下载govendor:
go get github.com/kardianos/govendor
下载完之后,到项目根目录下,执行初始化:
govendor init
govendor常用命令
| 命令 | 描述 |
|---|---|
| init | 初始换vendor目录 |
| list | 列出项目所有的依赖包 |
| add | 添加包到vendor目录下 |
| update | 更新GOPATH依赖包到vendor目录 |
| remove | 从vendor中删除依赖包 |
| status | 列出所有包的状态:缺失、过期或更改过的包 |
| fetch | 添加或者更新包到本地vendor目录 |
| sync | 根据vendor.json去更新依赖包 |
| get | 类似go get,拉取依赖包到vendor目录下 |
可以使用govendor fetch直接拉取源码下来:
govendor fetch github.com/go-gorm/gorm/
go mod必须是go 1.11版本及以上
先进行设置:
go env -w GOBIN=/Users/youdi/go/bin
go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct // 使用七牛云的
go mod命令
| 命令 | 说明 |
|---|---|
| download | download modules to local cache(下载依赖包) |
| edit | edit go.mod from tools or scripts(编辑go.mod) |
| graph | print module requirement graph (打印模块依赖图) |
| verify | initialize new module in current directory(在当前目录初始化mod) |
| tidy | add missing and remove unused modules(拉取缺少的模块,移除不用的模块) |
| vendor | make vendored copy of dependencies(将依赖复制到vendor下) |
| verify | verify dependencies have expected content (验证依赖是否正确) |
| why | explain why packages or modules are needed(解释为什么需要依赖) |
初始化项目:
// 进入到项目根目录,执行:
go mod init
// 或者 go mod init 项目名称
```
然后可以使用`go get`拉取需要依赖的包即可,会自动在go.mod中生成记录
- go mod download 下载模块到本地缓存,缓存路径是 $GOPATH/pkg/mod/cache
- go mod edit 是提供了命令版编辑 go.mod 的功能,例如 go mod edit -fmt go.mod 会格式化 go.mod
- go mod graph 把模块之间的依赖图显示出来
- go mod init 初始化模块(例如把原本dep管理的依赖关系转换过来)
- go mod tidy 增加缺失的包,移除没用的包
- go mod vendor 把依赖拷贝到 vendor/ 目录下
- go mod verify 确认依赖关系
- go mod why 解释为什么需要包和模块
一般我们可以在导入别人的项目,或者修改了go.mod文件后通过:
```
go mod tidy
```
来初始化后者刷新项目的依赖
如果使用goland来开发,那么需要设置下go module和go proxy代理:

边栏推荐
- Explanation of a textbook question
- 通达信股票账户开户安全吗
- 解决报错:Creating window glfw ERROR: GLEW initalization error: Missing GL version
- Rust,程序員創業的最佳選擇?
- OpenStack-----Nova源码分析之创建虚拟机
- Custom vertical table
- NVM installation and use tutorial
- How to configure aliases for typescript + vite projects
- Prototype relationship between constructor and instance (1)
- 分类器与cross entropy loss函数
猜你喜欢

OpenStack学习笔记(一)

k线图24种经典图解(影线篇)

权益NFT开创者Hash Eagle如何重新定义NFT,用权益赋能长续价值?

初始c语言时的一些知识

Method for wrapping multiple promise instances into a new promise instance

多臺雲服務器的 Kubernetes 集群搭建

The priority of catch() and then (..., ERR) of promise

楼宇自动化专用BACnet网关BL103

How to determine if a web worker has been created or closed

Where can the brightness of win7 display screen be adjusted
随机推荐
OpenStack学习笔记(一)
Data acquisition system gateway acquisition plant efficiency
1251- client does not support authentication protocol MySQL error resolution
SSH secret free function for # scripting
分类器与cross entropy loss函数
Replace element - counter use of content (2)
初始c语言时的一些知识
Kubernetes cluster construction of multiple ECS
Is it safe for Guosen Securities to open an account?
Rust, le meilleur choix pour un programmeur de démarrer une entreprise?
腾讯云搭建Socks5多IP代理服务器实现游戏单窗口单IP完美搭建教程附带工具「建议收藏」
通达信股票账户开户安全吗
Where can the brightness of win7 display screen be adjusted
完整详细的汇编实验报告
Some knowledge of the initial C language
Parabolic motion in unity 2D games
OpenStack学习笔记-Glance组件深入了解
JS prototype. slice. call(arguments); Convert pseudo array to array
Dialogue: recommended system quick start route and summary of knowledge points
多臺雲服務器的 Kubernetes 集群搭建