Boris Burkov
Boris Burkov

Reputation: 14506

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.

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?

Upvotes: 7

Views: 22322

Answers (4)

vendelin
vendelin

Reputation: 177

As of Go 1.18: 'go get' is no longer supported outside a module. To build and install a command, use 'go install' with a version, like 'go install example.com/cmd@latest' For more information, see https://golang.org/doc/go-get-install-deprecation or run 'go help get' or 'go help install'.

See: https://go.dev/doc/go-get-install-deprecation

BTW my own hack is a zsh alias function to git clone the dependency on GOPATH. This way you can use any new dependencies in any little tool you don't want to add a go.mod file:

gg () {
    base=$(echo "$1" | cut -d "/" -f1-2)
    cd $GOPATH/src && mkdir $base && cd $base && git clone "https://$1"
}

Upvotes: 0

bcmills
bcmills

Reputation: 5197

Dependency modules do not need to have explicit go.mod files.

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.

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).

However, in general a module that lacks an explicit go.mod file is valid and fine to use. Its effective module path is the path by which it was required, which can be a bit confusing if the same repository ends up in use via multiple paths. 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).

Upvotes: 9

Boris Burkov
Boris Burkov

Reputation: 14506

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 get can run in both ways: module-aware mode and legacy GOPATH mode (as of Go 1.16).

To read more about this, refer to docs by using the go command:

$ go help gopath-get

and

$ go help module-get

It'd tell about how go get works in both cases.

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.

I did a simple test to demonstrate the same:

$ go get github.com/mongoengine/mongoengine

And it surprisingly worked.

Upvotes: 3

Hymns For Disco
Hymns For Disco

Reputation: 8405

Modules are defined by their go.mod file. Without a go.mod file, it is not a module.

See this from the 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.

And

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

Upvotes: 1

Related Questions