Go Toolchain Basics
10 examples to get you started with Go Toolchain - 7 basic and 3 intermediate.
Prerequisites
- Install Go 1.26.x or later from go.dev/dl.
- Create a module:
mkdir tooldemo && cd tooldemo && go mod init example.com/tooldemo. - Save shell snippets in the module root unless a path is shown.
Basic Examples
1. Check toolchain version
Confirm which Go release is active before debugging build differences.
go version- Prints the compiler version and target OS/architecture.
- Compare output across laptop and CI when builds diverge.
go env GOVERSIONprints only the version string for scripts.
Related: The go Command: Build, Test, and Module Lifecycle - how the driver fits together
2. Build a binary
Compile the current package without installing globally.
go build -o bin/server ./cmd/servergo buildwith no-onames the binary after the directory (serverforcmd/server).- The command exits non-zero on compile errors; scripts should check
$?. - Add
-trimpathin release pipelines to strip file paths from binaries.
Related: go build, run, install & clean - flags and cache behavior
3. Run from source
Compile to a temp binary and execute in one step.
go run .- Ideal for local development; not for production deployment.
- Pass arguments after
--:go run . --port 8080. go run ./cmd/workertargets a specific package main.
Related: go build, run, install & clean - when to prefer
buildoverrun
4. Test a package
Run tests in the current directory.
go test -v .-vprints each test name as it runs.- Tests live in
*_test.gofiles beside production code. - Failed tests exit with status 1, which CI should treat as a hard gate.
Related: go test, list & generate - patterns, race, and benches
5. Test all packages recursively
Exercise the whole module tree.
go test ./..../...expands to every package under the module root.- Add
-count=1to disable test result caching when debugging flakes. - Combine with
-racein CI for data-race detection.
Related: go test, list & generate -
-race,-cover, and benchmarks
6. Initialize and inspect modules
go mod init creates the module root file.
go mod init example.com/tooldemo
cat go.mod- The module path should match your VCS hosting path when you publish.
go 1.26ingo.moddeclares the minimum language version.- One
go.modper module root; subpackages share it.
Related: go mod tidy, vendor, download & graph - keeping requires accurate
7. Tidy module dependencies
Sync go.mod with actual imports.
go mod tidy- Adds missing
requirelines and removes unused modules. - Updates
go.sumchecksum lines for every retained module. - Run after refactors that add or drop third-party imports.
Related: go mod tidy, vendor, download & graph - vendor and graph commands
Intermediate Examples
8. Static check with go vet
Catch suspicious constructs the compiler allows.
go vet ./...- Runs analyzers such as unreachable code and misused
fmtverbs. - Shares the type checker with
go test, so it compiles packages first. - Pair with golangci-lint in CI for broader rule sets.
Related: go fix Modernizers & go vet Analyzers - fix rewrites and custom analyzers
9. List packages and module versions
Inspect what the loader sees before a build.
go list ./...
go list -m -versions golang.org/x/syncgo listprints import paths, test status, and build tags applied.-mswitches to module mode for version queries.- Use
go list -m allto dump the full build list for audits.
Related: go test, list & generate - JSON output and
-ftemplates
10. Pin or switch toolchain with GOTOOLCHAIN
Control which Go release executes commands.
go env GOTOOLCHAIN
GOTOOLCHAIN=go1.26.0 go version- Default
automay download a newer patch that satisfiesgo.mod. GOTOOLCHAIN=localforbids automatic toolchain downloads.- Commit the
godirective ingo.modso teammates resolve the same baseline.
Related: Cross-Compilation & Private Module Proxies - env vars for builds and modules
Stack versions: This page was written for Go 1.26.x (Green Tea GC default, go fix modernizers - verify patch at build), chi (latest - verify at build), gin (latest - verify at build), echo (latest - verify at build), google.golang.org/grpc (latest - verify at build), sigs.k8s.io/controller-runtime (latest - verify at build), kubebuilder (latest - verify at build), tinygo (latest - verify board targets at build), wazero (latest - verify at build), and golangci-lint (latest - verify linter set at build).