我通过运行以下命令初始化一个go项目:
go mod init firstgo_app
我确认模块是创建的:
cat go.mod
module firstgo_app
go 1.18
然后,我在github.com/gonic/gin上安装了一个依赖项,执行
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
)
我不明白的是,所有的依赖都被标记为间接的。我的理解是,只有传递依赖被标记为间接的,但是我直接依赖的依赖不应该被标记为这种依赖。也就是说,github.com/gin-gonic/gin v1.7.7 // 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
}
当我试图构建它时,失败的原因是:
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
中的所有依赖项仍然是间接标记的。
那是怎么回事?我在这里错过了什么?还是我对间接的理解错了?
发布于 2022-05-08 18:05:49
你的理解是正确的。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
)
这是从去1.14开始
go mod tidy
以外的go命令不再编辑go.mod
文件,如果更改只是表面的。
https://stackoverflow.com/questions/72163772
复制相似问题