Skip to main content
In this lesson we’ll cover the most commonly used Go (Golang) CLI commands for module initialization, dependency management, building, running, and installing programs. Each command section explains what it does and includes concise examples you can copy and run. This guide is useful for both beginners and experienced developers who want a quick reference for daily Go workflows.

go mod init

Initializes a new module by creating a go.mod file in the current directory. You should provide the module path (for example, a repository path). This command sets the module root to the current directory and records the module path used for import resolution. Example:
If you run go mod init without an argument, Go will attempt to infer a module path from the current directory — however, providing an explicit module path is recommended for clarity and reproducibility.

go mod tidy

Keeps your go.mod and go.sum files in sync with the packages imported by your source code. Specifically, it will:
  • add any missing module requirements needed to build the module’s packages and dependencies,
  • remove requirements on modules that no longer provide relevant packages,
  • update go.sum accordingly.
Example:
Run this regularly (or in CI) to ensure minimal and correct dependency metadata.

go run

Compiles and runs Go programs without leaving a permanent binary in your source tree. go run builds a temporary executable, runs it, and deletes it when the program exits. It’s ideal for quickly testing small programs or running a single file. Examples:
Note: go run accepts one or more files or a package path.

go build

Compiles the packages named by the import paths along with their dependencies. For a package main, go build produces an executable. When building a single package, the resulting executable is placed in the current directory by default (unless you specify -o). Examples:
Common go build flags:
  • -o <file> : set output file name
  • -v : print package names as they are compiled
  • -x : print the full commands

go install

Compiles and installs the named packages. The installed binary is placed in GOBIN if set, otherwise in $GOPATH/bin. When that directory is on your PATH, the installed executable becomes available system-wide. Examples:
Check where Go will install binaries:
From Go 1.17 onward, installing specific versions of binaries is commonly done with go install <pkg>@<version>. Using go install without an @version will still install the package, but go install pkg@version is the recommended pattern for reproducible installs of command binaries.

go get

go get resolves the command-line arguments to packages at specific module versions, updates go.mod to require those versions, and downloads the source into the module cache. Typical uses include adding or updating dependencies and fetching modules referenced by go.mod. Examples:
Warning: For installing command binaries, prefer go install <pkg>@<version> over go get for reproducible installs and clearer intent.

Quick reference table

  • Use go mod init to start a new project. Commit the generated go.mod and go.sum.
  • Run go mod tidy before committing to keep dependency metadata clean.
  • Use go run for quick experimentation and go build for producing release binaries.
  • For installing CLI tools, prefer go install pkg@version to ensure reproducible installs.
  • Keep your GOBIN or $GOPATH/bin in your PATH to run installed binaries directly.
That covers the core commands you’ll use most often when working with Go modules, dependencies, and binaries.

Watch Video