# go — The Go Command-Line Tool

> Practical guide to the go toolchain: build, run, test and mod for modules, workspaces, cross-compiling and formatting.

Source: https://www.jpkc.com/db/en/cheatsheets/build-languages/go/

<!-- PROSE:intro -->
`go` is the single command-line tool that bundles the entire Go toolchain: you compile and run programs with `go run` and `go build`, run tests and benchmarks with `go test`, and manage your dependencies through Go modules (`go mod`). Instead of a separate build system or package manager, everything lives in this one binary – including formatting, static analysis, code generation and profiling. This guide walks you through the commands you reach for daily, from your first `go run main.go` to cross-compiling and module maintenance.
<!-- PROSE:intro:end -->

## Build & Run

`go run <file>` — Compile and run a Go program.

```bash
go run main.go
```

`go run .` — Compile and run the current package.

```bash
go run .
```

`go build` — Compile the current package.

```bash
go build
```

`go build -o <name>` — Compile with a custom output binary name.

```bash
go build -o myapp
```

`GOOS=<os> GOARCH=<arch> go build` — Cross-compile for a different OS/architecture.

```bash
GOOS=linux GOARCH=amd64 go build -o myapp-linux
```

`go install` — Compile and install the binary to $GOPATH/bin.

```bash
go install
```

## Modules

`go mod init <module>` — Initialize a new Go module.

```bash
go mod init github.com/user/myapp
```

`go mod tidy` — Add missing and remove unused dependencies.

```bash
go mod tidy
```

`go get <package>` — Add or update a dependency.

```bash
go get github.com/gin-gonic/gin@latest
```

`go get <package>@<version>` — Get a specific version of a dependency.

```bash
go get github.com/gin-gonic/gin@v1.9.1
```

`go mod download` — Download all dependencies to local cache.

```bash
go mod download
```

`go mod vendor` — Copy dependencies into a vendor directory.

```bash
go mod vendor
```

`go mod graph` — Print the module dependency graph.

```bash
go mod graph
```

## Test

`go test` — Run tests in the current package.

```bash
go test
```

`go test ./...` — Run tests in all packages recursively.

```bash
go test ./...
```

`go test -v` — Run tests with verbose output.

```bash
go test -v
```

`go test -run <regex>` — Run only tests matching a pattern.

```bash
go test -run TestParse
```

`go test -cover` — Show test coverage percentage.

```bash
go test -cover ./...
```

`go test -coverprofile=cover.out` — Generate coverage profile.

```bash
go test -coverprofile=cover.out && go tool cover -html=cover.out
```

`go test -bench .` — Run benchmarks.

```bash
go test -bench . -benchmem
```

## Format & Lint

`go fmt ./...` — Format all Go source files.

```bash
go fmt ./...
```

`gofmt -d <file>` — Show formatting diff without applying changes.

```bash
gofmt -d main.go
```

`go vet ./...` — Run static analysis to find potential bugs.

```bash
go vet ./...
```

`go generate ./...` — Run go:generate directives in source files.

```bash
go generate ./...
```

## Tools & Environment

`go env` — Show all Go environment variables.

```bash
go env
```

`go env GOPATH` — Show a specific environment variable.

```bash
go env GOROOT
```

`go env -w <KEY>=<value>` — Set a Go environment variable persistently.

```bash
go env -w GOPROXY=direct
```

`go version` — Show the installed Go version.

```bash
go version
```

`go doc <package>` — Show documentation for a package or symbol.

```bash
go doc fmt.Println
```

`go tool pprof <profile>` — Analyze a CPU or memory profile.

```bash
go tool pprof cpu.prof
```

<!-- PROSE:outro -->
## Conclusion

`go` unifies compiler, test runner, package manager and formatter into a single, quick-to-learn tool – and that's exactly what makes day-to-day Go so smooth. Stick to the module commands (`go mod init`, `go mod tidy`) and your dependency tree stays clean and reproducible. A word of caution: `go install` and `go get` download, compile and run third-party code along with any build steps – only pull modules you trust, and rely on the safety net of `GOPROXY` and `GOSUMDB`, which verify versions and guard against silent tampering. When builds behave strangely, a clean cache via `go clean -cache` often helps.

## Further Reading

- [go.dev/doc](https://go.dev/doc/) – official documentation for the language, toolchain and modules
- [Go Modules Reference](https://go.dev/ref/mod) – reference for modules, versioning, `GOPROXY` and `GOSUMDB`
<!-- PROSE:outro:end -->

## Related Commands

- [artisan](https://www.jpkc.com/db/en/cheatsheets/build-languages/artisan/) – command-line tool for the Laravel PHP framework
- [cargo](https://www.jpkc.com/db/en/cheatsheets/build-languages/cargo/) – build tool and package manager for Rust
- [composer](https://www.jpkc.com/db/en/cheatsheets/build-languages/composer/) – dependency manager for PHP

