go mod init creates a go.mod file in the current directory and turns that directory into the root of a Go module. You give it a module path, for example go mod init example.com/hello, and from then on every package below that directory is addressed by that path. Since Go 1.16 you need a module for almost every go build, go run and go test that touches more than a single loose file.
A module is a collection of packages that are versioned and released together. The go.mod file records the module's name, the Go version it targets and the other modules it depends on. This guide shows how to run go mod init, how to choose a good module path and what the generated file contains.
Before modules, Go code lived in a single GOPATH workspace and dependencies had no versions. Modules replaced that model. A module gives you:
GOPATH.The go command finds the current module by walking up from the working directory until it meets a go.mod file. That directory is the module root.
Create an empty folder and initialise it.
The command creates one file, go.mod. Now add a program in the same folder.
go run . builds the package in the current directory. Because a go.mod file exists, the tool knows which module the code belongs to and no extra setup is needed.
The module path is the prefix of every import path in your project. If your module is example.com/hello and you create a folder greet, other code imports it as example.com/hello/greet.
| Situation | Typical module path |
|---|---|
| Code you will publish on GitHub | github.com/yourname/hello |
| Code on your own domain | example.com/hello |
| Private company code | git.company.com/team/hello |
| Throwaway experiment | hello or example/hello |
Follow these rules:
go get, the path must match where the repository is hosted, because the tool downloads the code from that location.hello works for local practice, but it cannot be fetched by other projects.If you run go mod init with no argument inside a Git repository that has a remote, older tools sometimes guessed the path. Do not rely on guessing; always pass the path explicitly.
A fresh file is short.
module declares the module path.go states the minimum Go language version the module needs. go mod init writes the version of the toolchain you ran it with, so the number on your machine will differ.Later the file may also gain require lines for dependencies, an optional toolchain line, and directives such as replace and exclude. You normally do not write those by hand; commands such as go get and go mod tidy maintain them.
One module can hold many packages. Each folder under the module root is a package, and its import path is the module path plus the folder path.
The import example.com/hello/greet is resolved locally because it starts with the module path. No download happens and no network is needed.
go: go.mod already exists. Edit the existing file instead.go.mod files create separate modules.go mod init without an argument fails outside a version-controlled folder with a message asking for the path.module line and update every import that used the old prefix.No. go run main.go works on a single file with no go.mod. As soon as you import your own packages or any third-party code, create a module.
For code that stays on your machine, yes. For code other people will import with go get, it must be the URL where the code is hosted, without the https:// part.
At the root of the project, next to your top-level code. Every package in the sub-folders then belongs to the same module.
No. It only writes go.mod. Dependencies are added later with go get or go mod tidy.
go mod init <module-path> creates go.mod, the file that makes a folder a Go module. Choose a lowercase path that matches where the code will live, run the command once at the project root, and import your own packages with that path as the prefix. The generated file records the module name and Go version; later commands add dependencies for you.