惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
Lohrmann on Cybersecurity
I
Intezer
M
MIT News - Artificial intelligence
博客园 - 【当耐特】
Martin Fowler
Martin Fowler
G
GRAHAM CLULEY
Jina AI
Jina AI
Engineering at Meta
Engineering at Meta
AI
AI
SecWiki News
SecWiki News
C
Cybersecurity and Infrastructure Security Agency CISA
IT之家
IT之家
D
Darknet – Hacking Tools, Hacker News & Cyber Security
T
The Blog of Author Tim Ferriss
P
Proofpoint News Feed
S
Security Affairs
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Microsoft Azure Blog
Microsoft Azure Blog
P
Proofpoint News Feed
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Google DeepMind News
Google DeepMind News
H
Hacker News: Front Page
爱范儿
爱范儿
F
Fortinet All Blogs
大猫的无限游戏
大猫的无限游戏
V
V2EX
H
Help Net Security
The Hacker News
The Hacker News
G
Google Developers Blog
Latest news
Latest news
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Tor Project blog
宝玉的分享
宝玉的分享
博客园 - 司徒正美
H
Heimdal Security Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
MyScale Blog
MyScale Blog
S
SegmentFault 最新的问题
C
Cisco Blogs
Blog — PlanetScale
Blog — PlanetScale
S
Security @ Cisco Blogs
B
Blog RSS Feed
B
Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cyberwarzone
Cyberwarzone
L
LINUX DO - 最新话题
K
Kaspersky official blog
MongoDB | Blog
MongoDB | Blog

陈少文的网站

巨变与机遇的未来十年 Kubernetes 平台管理软件压力测试方案 使用镜像部署 Hexo 静态页面 终于等到你 - GitHub 镜像仓库服务(ghcr.io) 一起来学 Go --(6)Interface 一起来学 Go --(5)Goroutine 和 Channel 什么是函数式编程 如何在 Kubernetes 集群集成 Kata 柯里化与偏函数 使用 PyGithub 自动创建 Label 软件产品是团队能力的输出 Helm 2 、Helm 3 比较 IoT 变现 Kubernetes 中的 DNS 服务 国内的 Helm 镜像源 Harbor 使用自签证书支持 Https 访问 DevOps 工具链之 Prow 如何使用 kfctl 安装 Kubeflow VS Code 无法下载 Go 插件的工具包 工程师更应具有服务精神 你不知道的 Docker 使用技巧 使用 Docker 运行 Tensorflow 论中国 什么是左移 如何清空 Git 仓库全部历史记录 一禅小和尚 有风吹过厨房 时间的玫瑰 如何在 CentOS 安装 GPU 驱动 开发 Tips(19) 使用 Velero 备份 Kubernetes 集群 Kubernetes Cheat Sheet 开发 Tips(18) 如何构建一个 Java 工程 开发 Tips(17) KubeSpray 安装 Kubernetes 报错 ip in ansible_all_ipv4_addresses 基于 Kubernetes 和 Jenkins 搭建自动化测试系统 在 Kubernetes 上动态创建 Jenkins Slave 使用 Jenkins 进行服务拨测 开发 Tips(16) Kubernetes 签发 Ingress 证书及日常故障运维 Kubernetes 中 Deployment 的基本操作 Kubernetes 中的证书 如何使用 KubeBuilder 开发一个 Operator Kubernetes 1.6.0 安装问题汇总 镜像管理工具 -- Harbor 开发 Tips(15) Docker 如何拉取镜像 开发 Tips(14) 使用 Helm 安装 harbor 开发 Tips(13) 使用 S2I 构建云原生应用 在 Kubernetes 中使用 emptyDir、hostPath、localVolume 开发 Tips(12) 开发 Tips(11) 代码质量分析工具 SonarQube 使用 Kubeadm 安装 Kubernetes 集群 一起来学 Go --(4)常用函数 Kubernetes 中的 Ceph Kubernetes 之 Volumes Kubernetes 之 Labels、Selectors 开发 Tips(10) 开源正在重构商业模式 Kubernetes 之网络 Kubernetes 之 API 使用 Helm 和 Operator 快速部署 Prometheus Kubernetes 复杂有状态应用管理框架 -- Operator Kubernetes 的包管理器 -- Helm 如何一步一步地优化博客方案 kubectl 实用指南 Kubernetes 中的基本概念 搭建远程 Kubernetes 开发环境 大公司和小公司的 ToB 思路 开发 Tips(9) Go 入门指南 一起来学 Go --(2)数据与逻辑结构 如何预防 Web 富文本中的 XSS 攻击 django-xss-cleaner 云工作时代 一起来学 Go --(1)背景与特点 SaaS 开发团队的不同阶段 你不知道的 Git 使用技巧 输出既服务 微服务设计 继续奔跑 开发 Tips(8) 从账户安全到二次验证 Django 性能之数据库查询优化 Django 性能之分库分表 敏捷开发之研发流程 打造一致性的团队 开发 Tips(7) Pytest 进阶学习之 Mock PaaS 部署之 buildpack Go 开发配置 领域输出才是 PaaS 的核心竞争力 Pytest 入门学习 开发 Tips(6) 如何使用 Jenkins、Docker、GitLab 搭建 Django 自动化部署流程 开发 Tips(5)
一起来学 Go --(3)Go Modules
微信公众号 · 2019-07-23 · via 陈少文的网站

Please enable Javascript to view the contents

1. Go 的包管理机制

1.1 GOPATH

GOPATH 通过 go get 命令,拉取代码放到 GOPATH 目录中。

GOPATH 的问题是:

  • 不能进行包版本的管理
  • 使用全局仓库,不能有效进行隔离

1.2 Vendor

1.5 版本开始,Go 中加入了 Vendor 机制。Vendor 解决了 GOPATH 的部分问题。

Vendor 机制通过在项目目录下增加 vendor 文件夹,管理依赖包。

Vendor 的问题是:

  • 无法解决嵌套依赖
  • vendor 只在 GOPATH 路径下有效

1.3 Go modules

Go modules 允许项目代码放在任意目录,依赖包统一保存在 $GOPATH/pkg/mod 目录下,避免了 vendor 方案的重复代码。

2. Go modules 特征

Go 通过 GO111MODULE 开关变量,控制 Go modules 特性是否启用,可选值有三个 : auto/on/off 。1.11 版本开始引入,默认值为 auto,1.13 版本默认开启。

auto 的含义是,当前目录下有 go.mod 则开启。auto 模式下:

  • 在 GOPATH 目录下编译时,默认使用 vendor、GOPATH 进行包管理
  • 在 GOPATH 目录外编译时,默认使用 go.mod 设置进行包管理。

在 Go modules 使用过程中,会自动生成两个文件 go.sum 和 go.mod。通常,会将这两个文件也提交到代码仓库。

  1. go.mod

go.mod 记录了依赖包的版本信息和操作命令。go.mod 提供了 module、require、replace、exclude 四个命令。

1
2
3
4
cat go.mod
module github.com/project/hello
go 1.12
require rsc.io/quote v3.1.0+incompatible
  1. go.sum

go.sum 提供了版本校验值。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
cat go.sum
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c h1:qgOY6WgZOaTkIIMiVjBQcw
93ERBE4m30iBm00nkL0i8=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3
fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
rsc.io/quote v3.1.0+incompatible h1:5v8TkzZ3hgTuFV/P47Ib17+Lc9DHpLRa+HPYYhT/X9
o=
rsc.io/quote v3.1.0+incompatible/go.mod h1:LzX7hefJvL54yjefDEDHNONDjII0t9xZLPX
sUe+TKr0=
rsc.io/sampler v1.3.0 h1:7uVkIFmeBqHfdjD+gZwtXXI+RODJ2Wc4O7MPEh/QiW4=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=

3. 配置代理

在安装 Go 依赖包时,会对外网请求数据。有些包托管在 github.com ,还有些包托管在 golang.org 、k8s.gcr.io 等仓库。由于 google 相关的网址被屏蔽,经常会遇到网络无法访问的问题。

在 1.11 版本中,新增了一个环境变量 GOPROXY ,可以用来配置代码仓库镜像代理。

以配置 jfrog 提供的 GoCenter 为例,在运行环境执行:

1
export GOPROXY=https://gocenter.io

即可,使用 jfrog 提供的代理。除此,GoCenter 还提供了包搜索的功能。

当然,aliyun 也提供了 GOPROXY :

1
export GOPROXY=https://mirrors.aliyun.com/goproxy/

4. Go modules 基本命令

Go modules 帮组文档,已经描述得十分详细。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
go help 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(编辑 go.mod 文件)
	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(将依赖包复制到 vendor)
	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.
  1. 新建一个目录 hello,目录中新增文件 hello.go
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
cat hello.go
package main

import (
    "fmt"
    "rsc.io/quote"
)

func main() {
    fmt.Println(quote.Hello())
}
  1. 初始化包为仓库地址
1
go mod init github.com/project/hello
  1. 编译,生成可执行文件
  1. 将依赖包归档到项目 vendor 目录
1
2
3
4
5
6
7
8
go mod vendor -v
# golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c
golang.org/x/text/language
golang.org/x/text/internal/tag
# rsc.io/quote v3.1.0+incompatible
rsc.io/quote
# rsc.io/sampler v1.3.0
rsc.io/sampler
  1. 验证依赖
1
2
go mod verify
all modules verified
  1. 查看最终目录结构
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
tree -L 2
.
├── go.mod
├── go.sum
├── hello
├── hello.go
└── vendor
    ├── golang.org
    ├── modules.txt
    └── rsc.io

5. 参考


微信公众号