go modules包管理
记录一下go工程迁移go modules的过程。
go mod
golang从1.11版本之后引入了包管理-go mod,并通过环境变量GO111MODULE 设置:
- 默认GO111MODULE 为auto 在gopath路径下会从gopath 或者vendor中寻找依赖包,在外部会使用go module的方式寻找依赖包。
- GO111MODULE =on 只会使用go module的方式寻找依赖包。
- GO111MODULE =off 只会从gopath中寻找依赖包。
go mod 命令
go mod提供了以下的命令:
download download modules to local cache
edit edit go.mod from tools or scripts
graph print module requirement graph
init initialize new module in current directory
tidy add missing and remove unused modules
vendor make vendored copy of dependencies
verify verify dependencies have expected content
why explain why packages or modules are needed
go mod新建工程步骤
- 在工程根目录下(如果GO111MODULE 为auto则工程不可以在gopath中)
go mod init [module name]。在工程根目录下会产生一个go.mod文件。
go mod 初始化的时候会自动导入vendor目录中的包。
goland也支持使用go mod管理包,配置如图:

go mod 会贯穿go tool工具链,go test, go vet, go build等工具都会先检查依赖。go mod会自动工作。 - 执行
go build main.go会依次下载依赖包到gopath/pkg/mod中,并在go.mod中进行管理。
执行go mod vendor会将所有的依赖包复制到工程vendor目录中。
go.mod示例:
module server
go 1.12
require (
github.com/360EntSecGroup-Skylar/excelize/v2 v2.0.2
github.com/YoungPioneers/blog4go v0.5.9
github.com/a8m/kinesis-producer v0.2.0
github.com/antlr/antlr4 v0.0.0-20191115170859-54daca92f7b0 //
indirect
github.com/apache/thrift v0.13.0
github.com/astaxie/beego v1.12.0
github.com/aws/aws-dax-go v1.0.1
github.com/aws/aws-sdk-go v0.0.0-20180828194226-46ffe7480c9d
github.com/fortytw2/leaktest v1.3.0 // indirect
)
go mod 会默认拉取最新的relase tag,如果没有,便拉取最新的commit记录。并支持版本控制。
indirect表明是间接引用。
注意:
"If an old package and a new package have the same import path, the new package must be backwards compatible with the old package."
“如果旧软件包和新软件包具有相同的导入路径,则新软件包必须与旧软件包向后兼容。”
- If the module is version v2 or higher, the major version of the module must be included as a
/vNat the end of the module paths used in go.mod files (e.g.,module github.com/my/mod/v2, require github.com/my/mod/v2 v2.0.1) and in the package import path (e.g.,import "github.com/my/mod/v2/mypkg"). This includes the paths used in go get commands (e.g.,go get github.com/my/mod/v2@v2.0.1. Note there is both a /v2 and a @v2.0.1 in that example. One way to think about it is that the module name now includes the /v2, so include /v2 whenever you are using the module name). - If the module is version v0 or v1, do not include the major version in either the module path or the import path.
- In general, packages with different import paths are different packages. For example, math/rand is a different package than crypto/rand. This is also true if different import paths are due to different major versions appearing in the import path. Thus
example.com/my/mod/mypkgis a different package thanexample.com/my/mod/v2/mypkg, and both may be imported in a single build, which among other benefits helps with diamond dependency problems and also allows a v1 module to be implemented in terms of its v2 replacement or vice versa.
来自于golang wiki。
如果导入了不兼容的高版本的包时,需要在import时表明版本。例import("github.com/360EntSecGroup-Skylar/excelize/v2")(restful 风格)。
- 如果需要导入本地包,可以编辑go.mod文件,添加
replace github.com/gohouse/goroom => /path/to/go/src/goroom
,并且需要在本地包根目录下执行go mod init [package name]
否则go mod在本地包目录中找不到go.mod会报错。
也可以是用go mod edit编辑。 - 执行
go mod tidy增加丢失的module,去掉未用的module。 - 配置好以后最后再执行
go build main.go便会编译成功。
go modules包管理的更多相关文章
- [系列] 使用 go modules 包管理工具(一)
目录 概述 初始化 添加依赖包 go mod 命令 小结 源码地址 概述 我想实现一个开箱即用的 API 框架的轮子,这个轮子是基于 Gin 基础上开发的. 为什么是开箱即用,它会集成哪些功能? 以上 ...
- 使用 go modules 包管理工具(一)
概述 我想实现一个开箱即用的 API 框架的轮子,这个轮子是基于 Gin 基础上开发的. 为什么是开箱即用,它会集成哪些功能? 以上功能点,都是常用的,后期可能还会增加. 废话不多说,咱们开始吧. 创 ...
- golang包管理解决之道——go modules初探
golang的包管理是一直是为人诟病之处,从golang1.5引入的vendor机制,到准官方工具dep,目前为止还没一个简便的解决方案. 不过现在go modules随着golang1.11的发布而 ...
- Golang modules包依赖管理工具
初始化 执行go mod init module-name,其中module-name为包名字,执行完后会生成go.mod文件,如下 module module-name go 1.13 包管理 使 ...
- Python 包管理工具解惑
Python 包管理工具解惑 本文链接:http://zengrong.net/post/2169.htm python packaging 一.困惑 作为一个 Python 初学者,我在包管理上感到 ...
- node包管理器
NPM小结 nodejs的出现,可以算是前端里程碑式的一个事件,它让前端攻城狮们摆脱了浏览器的束缚,踏上了一个更加宽广的舞台.前端的可能性,从此更加具有想象空间. 随着一系列基于nodes的应用/ ...
- 程序包管理rpm、yum与简单编译安装程序
Linux程序包管理 Linux中软件的安装主要有两种形式:一种是直接下载源代码包自行编译后安装,另一种直接获取rpm软件包进行安装. 程序的组成部分: 二进制程序:程序的主体文件,比如我们运行一个l ...
- Linux程序包管理rpm与yum
Linux程序包管理 Linux中软件的安装主要有两种形式:一种是直接下载源代码包自行编译后安装,另一种直接获取rpm软件包进行安装. 程序的组成部分: 二进制程序:程序的主体文件,比如我们运行一个l ...
- 华为云提供针对Nuget包管理器的缓存加速服务
在Visual Studio 2013.2015.2017中,使用的是Nuget包管理器对第三方组件进行管理升级的.而且 Nuget 是我们使用.NET Core的一项基础设施,.NET的软件包管理器 ...
随机推荐
- HDU 6556 (2018CCPC吉林 B题)
### HDU 6556 题目链接 ### 题目大意: 给你四个国家的时区,告诉你 A 国家的时间,让你输出这时候在 B 国家的时间,还需要输出对于 A 国家来说这是 昨天.今天 还是 明天. 分析前 ...
- PHP echo一个对象报语法错误,为什么?
为什么直接echo一个对象就会报语法错误,而如果这个对象实现了__toString方法后就可以直接输出呢? 原因是echo本来可以打印一个对象,而且也实现了这个接口,但是PHP对其做了个限制,只有实现 ...
- SEH hook 的一种方法
Windows内核分析索引目录:https://www.cnblogs.com/onetrainee/p/11675224.html 技术学习来源:火哥(QQ:471194425) 该方法的一些原理暂 ...
- java高并发系列 - 第8天:线程组
线程组 我们可以把线程归属到某个线程组中,线程组可以包含多个线程以及线程组,线程和线程组组成了父子关系,是个树形结构,如下图: 使用线程组可以方便管理线程,线程组提供了一些方法方便方便我们管理线程. ...
- 动态类型dynamic转换为特定类型T的方案
需求场景:有时候我们抓到一段请求数据,JSON格式的字符串数据,需要放在接口里重现问题,我们就可能会用dynamic先接受数据,然后再转换成特定数据发出请求. 方案一:直接使用特定对象T,来接受请求数 ...
- 马蜂窝 iOS App 启动治理:回归用户体验
增长.活跃.留存是移动 App 的常见核心指标,直接反映一款 App 甚至一个互联网公司运行的健康程度和发展动能.启动流程的体验决定了用户的第一印象,在一定程度上影响了用户活跃度和留存率.因此,确保启 ...
- 易优CMS:小白学代码之notempty
[基础用法] 名称:notempty 功能:判断某个变量是否为空,可以嵌套到任何标签里面使用,比如:channel.type等 语法: {eyou:notempty name='$eyou.field ...
- 最好用的koa2+mysql的RESTful API脚手架,mvc架构,支持node调试,pm2部署。
#基于webpack构建的 Koa2 restful API 服务器脚手架 这是一个基于 Koa2 的轻量级 RESTful API Server 脚手架,支持 ES6, 支持使用TypeSc ...
- LD_PRELOAD & putenv() 绕过 disable_functions & open_basedir
这次TCTF中一道题,给出了一个PHP一句话木马,设置了open_basedir,disable_functions包含所有执行系统命令的函数,然后目标是运行根目录下的/readflag,目标很明确, ...
- windows linux 通过SSH X11Forwrding 使用图形化界面
有时候,我们需要在命令行中使用远程的GUI程序,这样我们就需要x11转发的来进行访问: Linux平台下不需要特别的配置,假如我们要远程的机器是centos机器,只要做如下配置即可: #vi /etc ...