我通过运行以下命令初始化go项目:

go mod init firstgo_app

我确认模块已创建:

cat go.mod
module firstgo_app

go 1.18

然后我在github上安装了一个依赖项.com/gin-gonic/gin-by-executing

get github.com/gin-gonic/gin

之后我看了go.mod的内容,这次是这样的:

cat go.mod
module firstgo_app

go 1.18

require (
    github.com/gin-contrib/sse v0.1.0 // indirect
    github.com/gin-gonic/gin v1.7.7 // indirect
    github.com/go-playground/locales v0.13.0 // indirect
    github.com/go-playground/universal-translator v0.17.0 // indirect
    github.com/go-playground/validator/v10 v10.4.1 // indirect
    github.com/golang/protobuf v1.3.3 // indirect
    github.com/json-iterator/go v1.1.9 // indirect
    github.com/leodido/go-urn v1.2.0 // indirect
    github.com/mattn/go-isatty v0.0.12 // indirect
    github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
    github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 // indirect
    github.com/ugorji/go/codec v1.1.7 // indirect
    golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect
    golang.org/x/sys v0.0.0-20200116001909-b77594299b42 // indirect
    gopkg.in/yaml.v2 v2.2.8 // indirect
)

我不明白的是,所有依赖项都被标记为indirect.我的理解是,只有传递依赖项被标记为indirect,但我直接依赖的依赖项不应该被标记为indirect.那就是github.com/gin-gonic/gin v1.7.7 // indirect不应该有indirect标签,因为这是我专门下载的依赖项.

我认为是这样的,因为我没有直接使用依赖项,所以我再次创建了模块,但也创建了一个main.go文件,其中我明确依赖gonic/gin:

cat main.go
package main
import "github.com/gin-gonic/gin"
func main() {
        r := gin.Default()
        r.GET("/ping", func(c *gin.Context) {
                c.JSON(200, gin.H{
                        "message": "pong",
                })
        })
        r.Run() // listen and serve on 0.0.0.0:8080
}

当我try 构建时,失败的原因是:

go build
main.go:2:8: no required module provides package github.com/gin-gonic/gin; to add it:
    go get github.com/gin-gonic/gin

但当我运行go get github.com/gin-gonic/gin然后构建时,go.mod中的所有依赖项仍然被标记为间接.

那又有什么好处呢?我错过了什么?还是我错误地理解了indirect

推荐答案

你的理解是正确的.indirect注释表示依赖项不是由您的模块直接使用的,而是由其他模块依赖项间接使用的.

当您第一次运行go get github.com/gin-gonic/gin时,模块将被下载,但由于您不使用它,它仍将被标记为indirect.

当您开始使用它时,它将不再是indirect,但go build不会自动更新go mod.

运行go mod tidy,然后它将不再被标记为indirect.

$ go mod tidy
$ cat go.mod
module firstgo_app

go 1.18

require github.com/gin-gonic/gin v1.7.7

require (
        github.com/gin-contrib/sse v0.1.0 // indirect
        github.com/go-playground/locales v0.13.0 // indirect
        github.com/go-playground/universal-translator v0.17.0 // indirect
        github.com/go-playground/validator/v10 v10.4.1 // indirect
        github.com/golang/protobuf v1.3.3 // indirect
        github.com/json-iterator/go v1.1.9 // indirect
        github.com/leodido/go-urn v1.2.0 // indirect
        github.com/mattn/go-isatty v0.0.12 // indirect
        github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
        github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 // indirect
        github.com/ugorji/go/codec v1.1.7 // indirect
        golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect
        golang.org/x/sys v0.0.0-20200116001909-b77594299b42 // indirect
        gopkg.in/yaml.v2 v2.2.8 // indirect
)

Go 1.14年以来:

如果更改只是表面性的,则除go mod tidy以外的go命令不再编辑go.mod文件.

Go相关问答推荐

Makefile:现有文件上没有这样的文件或目录,不加载环境变量

将Go程序导出到WASM—构建约束排除所有Go文件

+在具有html/模板Golang的Base64中

golang.org/x/oauth2 oauth2.Config.Endpoint.TokenURL mock:缺少access_token

将类型定义为泛型类型实例化

GoFR HTTP服务初始化中Open遥测传输和超时配置的说明

如何使用Gio设置标题栏图标

Kafka golang 生产者在错误后更改分区计数

为什么标准库中的 IsSorted 会反向迭代切片?

当我使用 CircleCI 构建 Go Image 时,我得到runtime/cgo: pthread_create failed: Operation not allowed

linter 警告:返回值被忽略

我无法使用反向代理更改主机标头

由于 main.go 文件中的本地包导入导致构建 docker 容器时出错

也许在 golang 中包(字符串和字符串类型不匹配)

如何使用 Docker 引擎 SDK 和 Golang 运行 docker 挂载卷

接受通道和切片的通用函数

使用 golang 生成 vim

Golang模板无法访问embedFS中的文件

在 docker kill --signal=SIGX 上以这种方式关闭容器内运行的 go 应用程序是否安全?

使用正则表达式拆分具有相同标题的数据块