go 模块可以没有 go.mod 文件吗?

Can a go module have no go.mod file?

我 运行 进入了一个似乎是 Go 模块的 repo,但是里面没有 go.mod 文件:github.com/confluentinc/confluent-kafka-go.

go 模块没有 go.mod 依赖文件是否可以,或者该库的作者还没有迁移到模块?

模块由它们的 go.mod 文件定义。没有 go.mod 文件,它不是模块。

Go Modules Reference

看到这个

A module is a collection of packages that are released, versioned, and distributed together. Modules may be downloaded directly from version control repositories or from module proxy servers.

A module is identified by a module path, which is declared in a go.mod file, together with information about the module's dependencies. The module root directory is the directory that contains the go.mod file.

A module is defined by a UTF-8 encoded text file named go.mod in its root directory.

讨论的简短摘要:

答案是“否”!

这个项目以前contains a set of go packages, but it is not a Go module as it doesn't contain go.mod file (although, it used to be a multi-module repo (Go)

go get 可以 运行 两种方式:模块感知模式和传统 GOPATH 模式(从 Go 1.16 开始)。

要了解更多相关信息,请使用 go 命令参阅文档:

$ go help gopath-get

$ go help module-get

它将说明 go get 在这两种情况下的工作原理。

此外,我注意到它可以下载任何存储库并将其视为 Go 包,即使它包含任意 Python 项目。

我做了一个简单的测试来证明这一点:

$ go get github.com/mongoengine/mongoengine

而且效果惊人。

依赖模块do not need to have explicit go.mod files.

模块模式下的“主模块”——即包含 go 命令工作目录的模块——必须有一个 go.mod 文件,以便 go命令可以找出该模块中包的导入路径(基于它的module path),这样它就有了一个地方来记录它一旦解决的依赖关系。

此外,使用 replace directives 插入的任何模块必须有 go.mod 个文件(以减少由于拼写错误或替换路径中的其他错误造成的混淆)。

但是,通常缺少显式 go.mod 文件的模块是有效的并且可以很好地使用。它的有效模块路径是 required 的路径,如果同一个存储库最终通过多个路径使用,这可能会有点混乱。由于没有 go.mod 文件的模块必然不会指定自己的依赖项,因此该模块的消费者将不得不自己填写这些依赖项(go mod tidy 将在消费者的文件中将它们标记为 // indirect go.mod 个文件)。