---
title: "用 go mod 初始化模块并加入依赖 - Angryecho"
description: "准备工作 安装 Go（go version 有输出）。 依据：Go 官方文档 Modules · Managing dependencies。[1] 核实日期：2026-10-24。"
canonical_url: "https://www.angryecho.com/c/Goyu_yan/p/P2TBBjZxMzHHBVXuW2nja"
meta:
  "og:description": "准备工作 安装 Go（go version 有输出）。 依据：Go 官方文档 Modules · Managing dependencies。[1] 核实日期：2026-10-24。"
  "og:image": "https://www.angryecho.com/_og/d/c_Post%2Ctitle_~5ZCQ5qe9572R%2Cdescription_~5ZCQ5qe95LiA5LiL77yM5bCx5LiK5ZCQ5qe9572R44CC%2CpostId_P2TBBjZxMzHHBVXuW2nja%2Crevision_~MjAyNi0xMC0yOFQwMjo1NjowMC4yNzc5NDRa%2Cp_Ii9jL0dveXVfeWFuL3AvUDJUQkJqWnhNekhIQlZYdVcybmphIg%2Cs_pI2TKCckDpqOnXSp.png"
  "og:image:height": "630"
  "og:image:type": image/png
  "og:image:width": "1200"
  "og:title": "用 go mod 初始化模块并加入依赖"
---

> Index of every page on this site: [llms.txt](https://www.angryecho.com/llms.txt)

# **用 go mod 初始化模块并加入依赖**

## 准备工作

- 安装 Go（ `go version` 有输出）。
- 依据：Go 官方文档 Modules · Managing dependencies。\[1]
- 核实日期：2026-10-24。

## 操作步骤

### 1、在项目根初始化模块

做什么：

```bash
mkdir hello && cd hello
go mod init example.com/hello
```

为什么：`go mod init` 生成 `go.mod`，声明模块路径。之后所有依赖版本都以模块为单位记录，便于复现构建。\[1]

怎么确认：目录出现 `go.mod`，首行是 `module example.com/hello`；`go env GOMOD` 指向该文件。

### 2、编写代码后用 tidy 同步依赖

做什么：写一个引用外部模块的 `.go` 文件，然后执行：

```bash
go mod tidy
go build
```

为什么：`tidy` 根据源码增补需要的依赖、移除不用的依赖，并维护 `go.sum` 校验信息。官方推荐用它保持模块文件与源码一致。\[1]

怎么确认：`go.mod` 出现 `require` 块；`go.sum` 非空；`go build` 成功且生成可执行文件。

### 3、显式加入或升级某个版本

做什么：

```bash
go get example.com/some/module@v1.2.3
go mod tidy
```

为什么：需要钉死版本、升级小版本或回退时，显式 `go get` 比只靠自动解析更可控。\[1]

怎么确认：`go.mod` 中对应模块版本变为指定版本；`go list -m all` 能列出该模块及其版本。

### 4、查看模块图（可选）

做什么：`go list -m all` 或 `go mod graph | head`

为什么：排查“到底引进了哪个传递依赖”时，列表/图比肉眼翻 `go.sum` 快。\[1]

怎么确认：输出包含直接依赖与传递依赖；升级后对比两次输出的差异。

Go 模块把“导入路径”和“版本选择”变成显式文件。`go.mod` 描述你想要的模块与版本约束，`go.sum` 则记录下载内容的校验和，用来发现被篡改或意外替换的依赖。

日常开发里，最常见的失败是模块路径和仓库真实路径不一致，或者私有库没配置 `GOPRIVATE` 导致去公共代理拉取失败。这两类问题都不是业务代码写错，而是模块元数据/环境没对齐。

提交代码前跑一次 `go test ./...` 和 `go mod tidy`，可以减少“我这边能编译、CI 却缺依赖”的扯皮。需要审查供应链时，再辅以 `go list -m all` 看传递依赖是否混进不熟悉的模块。

若依赖下载缓慢，优先检查代理与求和校验是否被拦截，而不是反复删掉 `go.sum` 碰运气。保留 `go.sum` 能让同事和 CI 用同一份校验结果。

## 注意事项

- 模块路径应与仓库真实导入路径一致，否则他人 `go get` 会失败。
- 私有模块配置 `GOPRIVATE`；不要把 token 写进脚本明文或提交进仓库。
- 提交时同时提交 `go.mod` 与 `go.sum`。

## 来源

> \[1] Go Docs · Managing dependencies · https://go.dev/doc/modules/managing-dependencies · 核实于 2026-10-24

#Go #go.mod #模块 #依赖 #教程

排序方式 :

### **成为第一条评论！**

Nobody's responded to this post yet. Add your thoughts and get the conversation going.

```json
{"@context":"https://schema.org","@graph":[{"@id":"https://www.angryecho.com/c/Goyu_yan/p/P2TBBjZxMzHHBVXuW2nja#post","@type":"DiscussionForumPosting","commentCount":0,"dateModified":"2026-10-28T02:56:00.277Z","datePublished":"2026-10-24T04:00:00.000Z","description":"准备工作 安装 Go（go version 有输出）。 依据：Go 官方文档 Modules · Managing dependencies。[1] 核实日期：2026-10-24。","headline":"用 go mod 初始化模块并加入依赖","isPartOf":"https://www.angryecho.com/c/Goyu_yan","mainEntityOfPage":"https://www.angryecho.com/c/Goyu_yan/p/P2TBBjZxMzHHBVXuW2nja","text":"准备工作\n\n\n\n\n\n安装 Go（go version 有输出）。\n\n\n\n依据：Go 官方文档 Modules · Managing dependencies。[1]\n\n\n\n核实日期：2026-10-24。\n\n操作步骤\n\n1、在项目根初始化模块\n\n做什么：\n\nmkdir hello && cd hello\ngo mod init example.com/hello\n\n为什么：go mod init 生成 go.mod，声明模块路径。之后所有依赖版本都以模块为单位记录，便于复现构建。[1]\n\n怎么确认：目录出现 go.mod，首行是 module example.com/hello；go env GOMOD 指向该文件。\n\n2、编写代码后用 tidy 同步依赖\n\n做什么：写一个引用外部模块的 .go 文件，然后执行：\n\ngo mod tidy\ngo build\n\n为什么：tidy 根据源码增补需要的依赖、移除不用的依赖，并维护 go.sum 校验信息。官方推荐用它保持模块文件与源码一致。[1]\n\n怎么确认：go.mod 出现 require 块；go.sum 非空；go build 成功且生成可执行文件。\n\n3、显式加入或升级某个版本\n\n做什么：\n\ngo get example.com/some/module@v1.2.3\ngo mod tidy\n\n为什么：需要钉死版本、升级小版本或回退时，显式 go get 比只靠自动解析更可控。[1]\n\n怎么确认：go.mod 中对应模块版本变为指定版本；go list -m all 能列出该模块及其版本。\n\n4、查看模块图（可选）\n\n做什么：go list -m all 或 go mod graph | head\n\n为什么：排查“到底引进了哪个传递依赖”时，列表/图比肉眼翻 go.sum 快。[1]\n\n怎么确认：输出包含直接依赖与传递依赖；升级后对比两次输出的差异。\n\nGo 模块把“导入路径”和“版本选择”变成显式文件。go.mod 描述你想要的模块与版本约束，go.sum 则记录下载内容的校验和，用来发现被篡改或意外替换的依赖。\n\n日常开发里，最常见的失败是模块路径和仓库真实路径不一致，或者私有库没配置 GOPRIVATE 导致去公共代理拉取失败。这两类问题都不是业务代码写错，而是模块元数据/环境没对齐。\n\n提交代码前跑一次 go test ./... 和 go mod tidy，可以减少“我这边能编译、CI 却缺依赖”的扯皮。需要审查供应链时，再辅以 go list -m all 看传递依赖是否混进不熟悉的模块。\n\n若依赖下载缓慢，优先检查代理与求和校验是否被拦截，而不是反复删掉 go.sum 碰运气。保留 go.sum 能让同事和 CI 用同一份校验结果。\n\n注意事项\n\n\n\n\n\n模块路径应与仓库真实导入路径一致，否则他人 go get 会失败。\n\n\n\n私有模块配置 GOPRIVATE；不要把 token 写进脚本明文或提交进仓库。\n\n\n\n提交时同时提交 go.mod 与 go.sum。\n\n来源\n\n\n\n[1] Go Docs · Managing dependencies · https://go.dev/doc/modules/managing-dependencies · 核实于 2026-10-24\n\n#Go #go.mod #模块 #依赖 #教程","url":"https://www.angryecho.com/c/Goyu_yan/p/P2TBBjZxMzHHBVXuW2nja","author":{"@type":"Person","name":"UID:61128834","url":"https://www.angryecho.com/u/NGA61128834"},"interactionStatistic":[{"@type":"InteractionCounter","interactionType":"https://schema.org/LikeAction","userInteractionCount":0},{"@type":"InteractionCounter","interactionType":"https://schema.org/DislikeAction","userInteractionCount":0},{"@type":"InteractionCounter","interactionType":"https://schema.org/ViewAction","userInteractionCount":0},{"@type":"InteractionCounter","interactionType":"https://schema.org/ShareAction","userInteractionCount":0}]}]}
```

## Sitemap

See the full [sitemap](https://www.angryecho.com/sitemap.md) for all pages.
