Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >新功能Go modules介绍

新功能Go modules介绍

作者头像
李海彬
发布于 2018-07-31 04:03:13
发布于 2018-07-31 04:03:13
1.2K00
代码可运行
举报
文章被收录于专栏:Golang语言社区Golang语言社区
运行总次数:0
代码可运行

原文作者:Jekyll

Definition

A module is a collection of related go packages. Modules are the unit of source code interchange and versionning.

Quick history

  • Go before 1.5: populating GOPATH with go get.
  • Go 1.5 and after: dependency vendoring is introduced.
  • vgo is proposed as a prototype for Go modules support.
  • Go 1.11 (beta): vgo is being merged and refined as go mod (experimental).

Terminology

This article refers to recurrent expressions. Let’s clarify them:

  • “Module root”: the directory containing the file named go.mod.
  • “Module path”: the import path prefix corresponding to the module root.
  • “Main module”: the module containing the directory where the go command is run.

Module structure

A module is a tree of Go source files to which is added a file named go.mod. It contains the module import name, and the declaration of dependency requirements, exclusions and replacements. Its content would look like this:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 1module my/thing
 2
 3require (
 4        one/thing v1.3.2
 5        other/thing v2.5.0 // indirect
 6        ...
 7)
 8
 9exclude (
10        bad/thing v0.7.3
11)
12
13replace (
14        src/thing 1.0.2 => dst/thing v1.1.0
15)

Note that a dependency not directly imported in the module’s source code by an import statement is indentified as indirect in the file.

A module can contain other modules, in which case their content is excluded from the parent module.

Alongside go.mod, a file named go.sum may be present. This file retains cryptographic checksums of module dependencies, if any. It is used to verify that cached dependencies meet module requirements.

A module root can reside anywhere on the filesystem, whatever is the current GOPATH.

Module dependencies

Dependencies are downloaded and stored in GOPATH/src/mod. A direct consequence is that the use of a vendor directory is now obsolete.

What does this new structure looks like? Suppose we are working on a module that depends on github.com/me/lib at version 1.0.0. For such a case, in GOPATH/src/mod we would find:

What we can observe is:

  • Dependencies source trees are placed at the root of this directory, with a slight change: the import path is suffixed with @version.
  • Source archives retrieved or built from VCS are stored in the download folder.
  • VCS data is stored in the vcs folder.

Enabling Go modules support

In Go 1.11beta2, the environment variable GO111MODULE controls whether module support is enabled or disabled. It accepts three values: on, off, auto (default).

If set to “on”, module support is enabled whatever path we are in.

If set to “off”, it is permanently disabled.

If unset or set to “auto”, module support is enabled outside of GOPATH only if the current directory is a module root or one of its subdirectories.

Integration

Go modules are integrated with Go tools, for instance upon invocation of commands such as go build, go install, go run, go test appropriate actions will fire up like populating the cache, creating or updating go.mod and go.sum etc.

Autoformat

You should never have to run these commands on your own since they are invoked by other commands, but for the sake of completeness, let’s mention that go mod -fmt is the equivalent of go fmt for go.mod and go.sum files and that go mod -fix do some smart things in order to keep go.mod clean, like:

  • Rewriting non-canonical version identifiers to semantic versioning form.
  • Removing duplicates.
  • Updating requirements to reflect exclusions.

Initialization

To create go.mod:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
1go mod -init

You may have to pass the command an import path with -module <path> if the module root lives outside a VCS.

For the sake of backward compatibility and in order to ease the transition process, module creation has support for popular dependency management tools like dep, glide, glock,godep and so on.

Synchronization

In order to clean up unused dependencies or to fetch new ones, use the sync option:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
1go mod -sync

Adding, excluding and replacing dependencies

Two possibilities: either edit go.mod by hand or use the CLI. The latter comes with the following commands:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 1# require a new dependency
 2go mod -require one/thing@version
 3
 4# drop a requirement
 5go mod -droprequire one/thing
 6
 7# exclude a dependency
 8go mod -exclude bad/thing@version
 9
10# drop an exclusion
11go mod -dropexclude bad/thing@version
12
13# replace a dependency
14go mod -replace src/thing@version=dst/thing@version
15
16# drop a replacement
17go mod -dropreplace src/thing@version

Dependency graph

To print the graph of module dependencies:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
1go mod -graph

Generating vendor

If for backward compatibility reasons you need to ship your application with vendoring, you can generate the vendor directory from go.mod thanks to:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
1go mod -vendor

Getting help

Don’t hesistate to refer to go help mod and go help modules for further details about Go module support!


版权申明:内容来源网络,版权归原创者所有。除非无法确认,我们都会标明作者及出处,如有侵权烦请告知,我们会立即删除并表示歉意。谢谢。

Golang语言社区

ID:Golangweb

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2018-07-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Golang语言社区 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
Go Modules 详解
Go 1.11 和 Go 1.12 包含了初步的 Go Modules 支持,且计划在 2019 年 8 月发布的 Go 1.13 会在所有开发过程中默认使用 Go Modules。
码农编程进阶笔记
2021/07/20
9450
Go Modules 详解
go mod 命令详解
一个模块是 Go packages 的集合,定义在项目根目录下的 go.mod 文件。go.mod 文件定义了模块的路径,这也是使用当前项目中包的导入路径。go.mod 文件还定义了模块的依赖项,这些是项目成功构建所需的其他模块。每个依赖项都被编写为模块路径和特定的语义版本。
恋喵大鲤鱼
2023/12/30
2.6K0
跳出Go module的泥潭
Go 1.11 前天已经正式发布了,这个版本包含了两个最重要的feature就是 module和web assembly。虽然也有一些简单的教程介绍了go module的特性,但是基本上都是hello world的例子,在实践的过程中, 很多人都在“拼命的挣扎”,包括我自己, 从一些qq群、github的issue, twitter上都可以看到大家茫然或者抱怨的语句。
李海彬
2018/09/29
1.7K0
跳出Go module的泥潭
浅谈GoPath和Go Modules包管理
从v1.5开始开始引入vendor模式,如果项目目录下有vendor目录,那么go工具链会优先使用vendor内的包进行编译、测试等
仙人技术
2021/08/31
1.3K0
浅谈GoPath和Go Modules包管理
Go Modules 终极入门
Go modules 是 Go 语言中正式官宣的项目依赖解决方案,Go modules(前身为vgo)于 Go1.11 正式发布,在 Go1.14 已经准备好,并且可以用在生产上(ready for production)了,Go 官方也鼓励所有用户从其他依赖项管理工具迁移到 Go modules。
madneal
2020/03/10
2K0
Go Modules 终极入门
[Golang]包管理
本文是本人在探索 Go 最新的包管理 Go Modules 的一些总结,希望能够更深入了解 Go 最新的包管理方式,以及在实际环境中将它很好的使用起来。
别打名名
2020/07/28
1.8K0
go mod使用
最近由于换工作,开始交接工作。整理以前的工作内容,由于组内就我一个在做go和大数据。 所以开发没有规划,当时是怎么快怎么来。go也是使用最传统的go path的方式管理的。都是手动管理依赖的。现在交接给他人,需要多人开发,发现很多问题。比如版本问题,各种依赖的问题等等。
若与
2020/05/18
1.8K0
Go的包管理工具(三):Go Modules
在前面的文章,我们先是介绍了Go 的几种包管理方式,然后具体介绍了一种包管理的工具:glide。随着 Go 1.11 的发布,官方的包管理工具 Go Modules 变得流行起来。在发布不久的 Go 1.12 版本中,增强了对 Go Modules 的支持。本文将会介绍如何在项目中安装和使用 Go Modules 。
aoho求索
2019/05/07
1.6K0
Go语言重新开始,Go Modules 的前世今生与基本使用
2020 年腾讯内部的一份开发者报告显示,Go 语言已经成为腾讯内部第二大后端开发语言,在腾讯每天有大量的 Go 开发者在做业务和平台开发,大量的团队和项目使用也暴露出一些问题,随着 Go Modules 的出现,类似于内部自签发证书、安全审计等这些问题也逐渐得到解决。
Bain
2021/11/02
8210
Go语言重新开始,Go Modules 的前世今生与基本使用
go module
go 1.5 引进了vendor管理工程依赖包,但是vendor的存放路径是在GOPATH底下,另外每个依赖还可以有自己的vendor,通常会弄得很乱,尽管dep管理工具可以将vendor平级化管理,但是相对GOPATH的路径是逃不掉的。另外,各个包的版本管理也显得原始,甚至有的开发将依赖包从github直接download下来自己放到GOPATH底下的vendor。go的依赖包管理一致是开发者诟病的一个痛点。所以在千呼万唤中,go 1.11 终于引进了go module管理工程的包依赖,去除了项目包管理对GOPATH的依赖,明确了依赖包的版本管理。
用户2937493
2019/09/10
1.4K0
go module
Go Module详细使用教程,包管理不在难
go modules是 golang 1.11引入的新特性。模块是相关Go包的集合。modules是源代码交换和版本控制的单元。go命令直接支持使用modules,包括记录和解析对其他模块的依赖性。modules替换旧的基于GOPATH的方法来指定在给定构建中使用哪些源文件。
公号:咻咻ing
2020/03/04
20.7K0
Go源码编译
This is a part of a larger effort that I’m calling GopherSource, and our goal is for more of the gopher community to become upstream Go contributors.
李海彬
2018/07/26
8210
Go源码编译
Golang中的包管理工具 - Go Modules
在Go1.5之前使用GOROOT和GOPATH这2个系统环境变量来决定包的位置,对于开发者主要使用GOPATH。GOPATH 解决了第三方源码依赖的问题,看一下我本机 $GOPATH/src 下的目录:
猿哥
2019/08/01
1.6K0
Go Modules知识点总结
起初Go语言在1.5之前没有依赖管理工具,若想引入依赖库,需要执行go get命令将代码拉取放入GOPATH/src目录下,作为GOPATH下的全局依赖,这也就意味着没有版本控制及隔离项目的包依赖;
Golang梦工厂
2023/02/26
9300
Go Modules知识点总结
深入理解 Go Modules 的 go.mod 与 go.sum
流行的现代编程语言一般都提供依赖库管理工具,如 Java 的 Maven 、Python 的 PIP、Node.js 的 NPM 和 Rust 的 Cargo 等。Go 最为一门新生代语言,自然也有其自己的库管理方式。
恋喵大鲤鱼
2022/06/12
15.8K0
深入理解 Go Modules 的 go.mod 与 go.sum
使用 Go Modules 管理依赖
Go Modules 是 Go 语言的一种依赖管理方式,该 feature 是在 Go 1.11 版本中出现的,由于最近在做的项目中,团队都开始使用 go module 来替代以前的 Godep,Kubernetes 也从 v1.15 开始采用 go module 来进行包管理,所以有必要了解一下 go module。go module 相比于原来的 Godep,go module 在打包、编译等多个环节上有着明显的速度优势,并且能够在任意操作系统上方便的复现依赖包,更重要的是 go module 本身的设计使得自身被其他项目引用变得更加容易,这也是 Kubernetes 项目向框架化演进的又一个重要体现。
田飞雨
2019/12/20
1.1K0
Go mod包依赖管理工具使用详解
对比上面几点: 目前做的最好的也就 maven了,gradle没有使用过,不知道。
OwenZhang
2021/12/08
1.1K0
Go mod包依赖管理工具使用详解
一文读懂Go Modules原理
点击上方“腾讯云TStack”关注我们 获取最in云端资讯和海量技术干货 本文作者 / 阿杜 腾讯云高级工程师 热衷于开源、容器和Kubernetes 目前主要从事镜像仓库、边缘计算 以及云原生架构相关研发工作 1 前 言 In the world of software management there exists a dreaded place called “dependency hell.” The bigger your system grows and the more packag
腾讯云TStack
2021/03/01
3.1K0
手把手教你怎么导入Go语言第三方库
GO的命令go get让我们可以方便快捷的从网络中下载或更新Go语言包及其依赖文件,并将他们编译和安装。
Regan Yue
2021/09/16
5.7K0
手把手教你怎么导入Go语言第三方库
Go包管理工具
想想Java的Maven, Nodejs的NPM,还有我们赞颂一万遍也不过分的Python包管理,为什么生命总要浪费在这些事情上面呢? 陷入了深深的沉思~~
happy123.me
2019/12/30
7480
相关推荐
Go Modules 详解
更多 >
LV.1
Golang语言社区站长
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验