Go builds and packages programs with a single tool, go, and a module system. go build compiles an executable or checks a library, go install places a binary in your GOBIN, and libraries are distributed as modules that other developers fetch by version tag from a repository through the module proxy. No separate packaging tool is needed.
This guide covers modules, build commands, cross-compilation, build flags and how to publish a library or a binary.
Every project is a module described by a go.mod file. Create one at the project root:
The module path is the import prefix for every package inside. The go line states the minimum language version. Dependencies are added automatically when you import them and run go mod tidy, which also removes unused ones and updates go.sum with cryptographic checksums.
A package named main with a func main() produces an executable. Any other package is a library.
| Command | What it does |
|---|---|
go build | Compiles the current package. Writes a binary for main packages, discards the result for libraries (compile check) |
go build -o bin/app ./cmd/app | Writes the binary to a chosen path |
go install ./cmd/app | Builds and installs into GOBIN (default $GOPATH/bin) |
go install example.com/tool@latest | Installs a tool from a remote module without editing your go.mod |
go run . | Compiles to a temporary file and runs it |
go vet ./... | Reports suspicious constructs |
go test ./... | Builds and runs tests |
Use the ./... pattern to build every package in the module.
Go compiles for another operating system and architecture by setting two environment variables, with no extra toolchain for pure Go code.
List valid combinations with go tool dist list. Programs that use cgo need a C cross compiler, so for static, portable binaries set CGO_ENABLED=0.
-trimpath removes local file system paths from the binary, which helps reproducible builds.-ldflags="-s -w" strips the symbol table and DWARF debug data to shrink the file.-X main.version=1.4.0 sets a string variable at link time, a common way to embed a version.-race enables the data race detector, for tests and staging rather than production.-tags selects files with matching build constraints such as //go:build integration.Since Go 1.18 the binary also embeds module and VCS information, which you can read with go version -m ./app or debug.ReadBuildInfo().
Publishing a library means pushing the module to a version control host and tagging it with a semantic version.
github.com/you/strutil.go.mod, go.sum and the code.git tag v1.0.0 && git push origin v1.0.0.go get github.com/you/[email protected].The public module proxy proxy.golang.org caches the module after the first request, and pkg.go.dev generates documentation from your doc comments. For major versions 2 and above, the module path must end in /v2, /v3 and so on, for example module github.com/you/strutil/v2.
Keep implementation details in an internal/ directory. The compiler forbids importing internal packages from outside the parent module, which keeps your public API small.
For command line tools there are three common routes.
go install github.com/you/tool/cmd/tool@latest.go mod vendor copies dependencies into vendor/ for offline or audited builds.go work init ./a ./b creates a go.work workspace so several local modules build together without replace directives.GOPRIVATE=github.com/yourorg/* so the tool skips the public proxy and checksum database.go build writes the binary into the current directory or a path you choose. go install builds and moves the result to GOBIN, so the command is available on your PATH.
Not for building your code. Modules replaced GOPATH mode, and GO111MODULE defaults to on. GOPATH is still used for the module cache and the default GOBIN.
Use -ldflags="-s -w" and -trimpath, avoid pulling in large dependencies, and optionally compress with UPX if startup time and antivirus tooling are acceptable.
Since Go 1.24 you can record tools in go.mod with the tool directive via go get -tool, then run them with go tool name.
Go's toolchain handles the full path from source to distribution. Modules define identity and dependencies, go build and go install produce binaries, GOOS and GOARCH cross-compile, link flags embed versions, and a tagged repository is all a library needs to be consumable. Add GoReleaser or a container build when you ship executables to other people.