go mod init: create a Go module (go.mod)
The commands on this page have not been executed by our verification runner yet.updated
- tool
- Go
- versions
- Go 1.16+ (modules on by default)
- os
- any
go mod init example.com/hello creates a go.mod file in the current directory, which makes that directory the root of a Go module named example.com/hello. The argument is the module path: the import prefix for the module's packages, and normally the repository location (github.com/USER/REPO) if you plan to publish it. Run it once per project, then go mod tidy to record dependencies.
Create a module
mkdir hello && cd hello
go mod init example.com/helloThe new go.mod contains the module path and a go directive with the Go version of the toolchain that created it:
module example.com/hello
go 1.NN(1.NN stands for your installed Go version.)
Add dependencies
# write code that imports a package, then:
go mod tidy # adds missing requirements to go.mod and go.sum, removes unused ones
go get example.com/pkg@v1.2.3 # add or change one dependency explicitlygo.sum holds checksums of the dependency versions. Commit both go.mod and go.sum.
Choosing the module path
| Project | Module path |
|---|---|
| Will be published on GitHub | github.com/USER/REPO |
| Private repository | The repository URL without the scheme, e.g. git.example.com/team/app |
| Local experiment, never imported by others | Any name, e.g. example/hello |
| Major version 2 or later | Path ends in /v2, /v3 ... (e.g. github.com/USER/REPO/v2) |
Common errors
go: cannot determine module path for source directory ... (outside GOPATH, module path must be specified): rungo mod initwith an explicit module path.go: ... go.mod already exists: the directory is already a module; editgo.mod(or usego mod edit -module NEW_PATH) instead.
Sources
Related pages
errors · goGo: declared and not used: xGo refuses to compile a function that declares a local variable it never reads. Use the variable, delete it, or assign it to the blank identifier _ to debug.commands · uvuv python pin 3.13: set the project's Python version (.python-version)uv python pin 3.13 writes a .python-version file so uv run, uv sync and uv venv use that Python, downloading it if missing. Pin formats and requires-python.commands · cargocargo add serde --features derive: add a dependency with featurescargo add NAME --features F adds a crate to Cargo.toml with chosen features (Cargo 1.62+). Pin versions, dev and build deps, default features, and cargo remove.commands · gogo mod tidy: add missing and remove unused Go module dependenciesgo mod tidy adds missing module requirements, removes unused ones and updates go.sum. Flags such as -diff, -v and -go, how it treats build tags, and CI checks.