> For the complete documentation index, see [llms.txt](https://golang-2.gitbook.io/handbook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://golang-2.gitbook.io/handbook/xiang-mu-guan-li/gomod.md).

# 包管理

### 模块初始化

以一个简单的iris网站为例

{% code title="main.go" %}

```go
package main

import "github.com/kataras/iris"

func main() {
	app := iris.Default()
	app.Get("/hello", func(ctx iris.Context) {
		ctx.JSON(iris.Map{
			"say": "hello",
		})
	})
	// listen and serve on http://localhost:8080.
	app.Run(iris.Addr("localhost:8080"))
}
```

{% endcode %}

在当前目录中，执行以下命令进行初始化：

```
go mod init
```

或者指定模块名：

```
go mod init {{ module名称 }}
```

在不指定module名称的时候默认使用目录名作为module名称

初始化完成后会在当前目录中生成一个go.mod文件，格式如下：

{% code title="go.mod" %}

```go
module {{ module名称 }}

go {{ golang版本 }}
```

{% endcode %}

使用以下命令生成包依赖并下载依赖包到$GOPATH中

```
go get -u -v
```

依赖包下载后即可执行go build或go run。也可以在还未执行go get的情况下执行go build或go run，这两个指令会事先检查go.mod和本地的依赖包，如果存在缺少依赖包的情况会自动进行go get。

go mod获取依赖包的版本策略是优先获取最新的release，在没有release存在的情况下会获取最新的commit。

下载的依赖包存放在$GOPATH/pkg/mod/目录下。

### 模块精简

```
go mod tidy
```

该命令会将go.mod中缺失的模块补全并移除不需要的模块


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://golang-2.gitbook.io/handbook/xiang-mu-guan-li/gomod.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
