Go Module 简明教程

栏目: Go · 发布时间: 7年前

内容简介:用惯了dep,突然换到首先我们执行一下看看有什么效果:翻译一下:

用惯了dep,突然换到 go mod ,真是不习惯。

首先我们执行一下看看有什么效果:

$ go mod
Go mod provides access to operations on modules.

Note that support for modules is built into all the go commands,
not just 'go mod'. For example, day-to-day adding, removing, upgrading,
and downgrading of dependencies should be done using 'go get'.
See 'go help modules' for an overview of module functionality.

Usage:

    go mod <command> [arguments]

The commands are:

    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

Use "go help mod <command>" for more information about a command.

翻译一下:

  • 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 命令在 $GOPATH 里默认是执行不了的,因为 GO111MODULE 的默认值是 auto 。默认在 $GOPATH 里是不会执行, 如果一定要强制执行,就设置环境变量为 on

  • go mod init 在没有文件的时候是执行不了的,有文件也执行不了。除非在 main.go 里加上导入声明,例如

$ cat main.go
package main

func main() {
    println("Hello world")
}
$ go mod init
go: cannot determine module path for source directory /Users/jiajun/hello (outside GOPATH, no import comments)
$ vim go.mod
$ cat go.mod
module github.com/jiajunhuang/hello
$ go mod init
go mod init: go.mod already exists
$ rm go.mod
$ vim main.go
$ cat main.go
package main // import "github.com/jiajunhuang/hello"

func main() {
    println("Hello world")
}
$ go mod init
go: creating new go.mod: module github.com/jiajunhuang/hello
$ ls
go.mod  main.go
$ cat go.mod
module github.com/jiajunhuang/hello

当然,如果在已有代码的仓库里执行是不存在这个问题的。

go moddep 比较

  • go mod 支持代理,以后就可以使用私有镜像源了~,具体请搜索 GOPROXY
  • go mod 速度比 dep 快很多
  • go.mod 中列出了所有的依赖,这一点其实我不是很喜欢,因为当项目一大,历史一久,只要升级其中一个依赖,很可能整个依赖 就挂了。我还是比较喜欢只要列出顶级依赖,由程序处理子依赖的情况。

一个生成的 go.mod 的示例

module github.com/my/module/v3  // 这是你的包的声明

// require 里是依赖。需要带上路径和版本。
require (
    github.com/some/dependency v1.2.3
    github.com/another/dependency v0.1.0
    github.com/additional/dependency/v4 v4.0.0
)

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

快速转行做产品经理

快速转行做产品经理

李三科 / 华中科技大学出版社 / 2018-6-1 / 39.90

互联网已经进入以产品为中心的时代,不懂技术一样做高薪产品经理。本书将满足你转行、就业、加薪的愿望。 . 作者李三科,互联网资深产品经理。2011年离开传统销售行业进入互联网行业工作,从对产品经理的工作一无所知,到成长为一名年薪几十万的资深产品经理,他对产品经理职业有着深刻的理解,也积累了丰富的学习、工作经验。本书以作者亲身经历为线索,讲解学习产品经理相关知识和工作方法的经验,同时介绍求......一起来看看 《快速转行做产品经理》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

在线进制转换器
在线进制转换器

各进制数互转换器

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具