> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Golang commands

> A concise guide to common Go CLI commands for modules, dependency management, building, running, and installing Go programs with examples and recommended workflows

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:

```bash theme={null}
go mod init example.com/my/module
```

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:

```bash theme={null}
go mod tidy
```

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:

```bash theme={null}
# Run a single file
go run main.go

# Run the package in the current directory
go run .

# Run a sub-package (common for multi-command projects)
go run ./cmd/myapp
```

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:

```bash theme={null}
# Build the package in the current directory
go build

# Build and specify output filename
go build -o myapp

# Build all packages in the module (recursive)
go build ./...
```

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:

```bash theme={null}
# Install the package in the current module (package must be main)
go install

# Install a specific package by import path
go install example.com/cmd/mytool

# Preferred way to install a specific version of a binary:
go install example.com/cmd/mytool@v1.2.3
```

Check where Go will install binaries:

```bash theme={null}
go env GOPATH
go env GOBIN
```

<Callout icon="lightbulb" color="#1CB2FE">
  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.
</Callout>

## 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:

```bash theme={null}
# Add or upgrade to latest
go get example.com/pkg@latest

# Add or set to a specific version
go get example.com/pkg@v1.2.3

# Fetch/update dependencies referenced in go.mod
go get
```

Warning: For installing command binaries, prefer `go install <pkg>@<version>` over `go get` for reproducible installs and clearer intent.

## Quick reference table

| Command | Purpose | Example |
| - | - | - |
| `go mod init` | Initialize a new module and create `go.mod` | `go mod init example.com/my/module` |
| `go mod tidy` | Add/remove module requirements and update `go.sum` | `go mod tidy` |
| `go run` | Compile and run sources without a permanent binary | `go run main.go` / `go run .` |
| `go build` | Compile packages and produce an executable | `go build -o myapp` |
| `go install` | Build and install binaries to `GOBIN` or `$GOPATH/bin` | `go install example.com/cmd/mytool@v1.2.3` |
| `go get` | Add/upgrade module dependencies or fetch referenced modules | `go get example.com/pkg@v1.2.3` |

## Recommended workflows and tips

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

## Links and references

* [Official Go Modules Reference](https://go.dev/ref/mod)
* [Go CLI Documentation (go command)](https://go.dev/doc/cmd)
* [Go Blog: Modules (v2)](https://go.dev/blog/modules)

That covers the core commands you'll use most often when working with Go modules, dependencies, and binaries.

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/advanced-golang/module/6ce7d245-515e-428d-9672-915e49a8ebd6/lesson/775d4650-9396-4b84-984d-7f3149fea976" />
</CardGroup>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.