简体   繁体   English

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

[英]Can a go module have no go.mod file?

I ran into a repo that seems to be a Go module, but there's no go.mod file in it: github.com/confluentinc/confluent-kafka-go.我遇到了一个似乎是 Go 模块的存储库,但是其中没有go.mod文件:github.com/confluent-kafka/

Is it ok for a go module to have no go.mod file with dependencies, or the authors of that library just didn't migrate to modules yet? go 模块没有具有依赖关系的go.mod文件是否可以,或者该库的作者还没有迁移到模块?

Dependency modules do not need to have explicit go.mod files .依赖模块不需要有明确go.mod文件

The “main module” in module mode — that is, the module containing the working directory for the go command — must have a go.mod file, so that the go command can figure out the import paths for the packages within that module (based on its module path ), and so that it has a place to record its dependencies once resolved.模块模式下的“主模块”——即包含go命令的工作目录的模块——必须有一个go.mod文件,以便go中的基于命令的包的导入路径在它的模块路径上),这样它就可以在解决后记录其依赖关系。

In addition, any modules slotted in using replace directives must have go.mod files (in order to reduce confusion due to typos or other errors in replacement paths).此外,使用replace指令插入的任何模块都必须具有go.mod文件(以减少由于替换路径中的拼写错误或其他错误造成的混乱)。

However, in general a module that lacks an explicit go.mod file is valid and fine to use.但是,通常缺少显式go.mod文件的模块是有效的并且可以很好地使用。 Its effective module path is the path by which it was require d, which can be a bit confusing if the same repository ends up in use via multiple paths.它的有效模块路径是它被require d 的路径,如果同一个存储库最终通过多个路径使用,这可能会有点混乱。 Since a module with no go.mod file necessarily doesn't specify its own dependencies, consumers of that module will have to fill in those dependencies themselves ( go mod tidy will mark them as // indirect in the consumer's go.mod file).由于没有go.mod文件的模块不一定指定其自己的依赖项,因此该模块的使用者必须自己填写这些依赖项( go mod tidy将在使用者的go.mod文件中将它们标记为// indirect )。

Modules are defined by their go.mod file.模块由其 go.mod 文件定义。 Without a go.mod file, it is not a module.如果没有 go.mod 文件,它就不是一个模块。

See this from the Go Modules Reference请参阅Go 模块参考

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.模块由模块路径标识,该路径在 go.mod 文件中声明,以及有关模块依赖项的信息。 The module root directory is the directory that contains the go.mod file.模块根目录是包含 go.mod 文件的目录。

And

A module is defined by a UTF-8 encoded text file named go.mod in its root directory.模块由其根目录中名为 go.mod 的 UTF-8 编码文本文件定义。

SHORT SUMMARY OF THE DISCUSSION:讨论的简短摘要:

The answer is "No"!答案是不”!

This project 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) previously).该项目 包含一组 go 包,但它不是 Go 模块,因为它不包含go.mod文件(尽管( 转)它以前使用过的多模块)。

go get can run in both ways: module-aware mode and legacy GOPATH mode (as of Go 1.16). go get可以两种方式运行:模块感知模式和传统 GOPATH 模式(从 Go 1.16 开始)。

To read more about this, refer to docs by using the go command:要了解更多信息,请使用go命令参考文档:

$ go help gopath-get

and

$ go help module-get

It'd tell about how go get works in both cases.它会告诉go get在这两种情况下如何工作。

Also, I noticed that it can download any repository and would treat it as a Go package, even if it contains an arbitrary Python project.另外,我注意到它可以下载任何存储库并将其视为 Go package,即使它包含任意 Python 项目。

I did a simple test to demonstrate the same:我做了一个简单的测试来证明这一点:

$ go get github.com/mongoengine/mongoengine

And it surprisingly worked.它出人意料地奏效了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM