Language cheatsheet
Go 1 Cheatsheet
The go command for modules, building, testing and cross-compiling, plus the code patterns you write every day.
Setup and running
- Check the Go version — Print the installed Go toolchain version.
- Show Go environment settings — Print one environment setting, here the module cache and workspace location.
- Start a module — Create a go.mod file. Use the path where the module will be published as its name.
- Run the program — Compile and run the main package in the current folder.
- Build a binary — Compile the package into an executable named myapp.
- Install a command-line tool — Build and install a program from a module into your Go bin folder.
Modules and dependencies
- Add a dependency — Add a module at its latest version to go.mod.
- Tidy go.mod — Add missing modules and remove unused ones. Run it after changing imports.
- Download all dependencies — Download modules to the module cache, as CI and Docker builds do.
- Update all dependencies — Upgrade the dependencies of your packages to their newest minor or patch versions.
- See available updates — List modules that have newer versions.
- Find out why a module is needed — Show the import chain that requires a module.
- Print the dependency graph — Print each module and the modules it requires.
- Use a local copy of a module — Point a dependency at a folder on disk while you develop it.
- Vendor dependencies — Copy dependencies into a vendor folder for reproducible offline builds.
- Create a workspace — Work on several modules together without editing their go.mod files.
Testing and profiling
- Run all tests — Run the tests in every package of the module.
- Run tests verbosely — Print the name and result of every test.
- Run matching tests — Run tests whose names match a regular expression, in one package.
- Detect data races — Run the tests with the race detector turned on.
- Bypass the test cache — Run tests again even if nothing changed.
- Measure coverage — Print the percentage of statements covered by tests.
- View coverage in the browser — Write a coverage profile and open an annotated HTML report.
- Run benchmarks — Run benchmarks and report memory allocations. Tests are skipped.
- Fuzz a function — Run a fuzz test for 30 seconds to look for inputs that make it fail.
- Profile a running service — Capture 30 seconds of CPU profile from a service that exposes net/http/pprof.
Quality and release builds
- Format the code — Rewrite files in the standard Go style.
- Find likely mistakes — Report suspicious constructs such as wrong Printf formats.
- Read documentation — Show the documentation for a package or symbol in the terminal.
- Run code generators — Run //go:generate directives found in the source.
- Cross-compile for Linux — Build for another operating system and architecture.
- Build a small static binary — Disable cgo and strip debug information for a compact, portable binary.
- Set a version at build time — Set a string variable in package main from the command line.
- Clear the build cache — Delete cached build results, for example to rule out stale artifacts.
Code patterns
- Hello world — The smallest runnable Go program.
- HTTP server — Start a web server with only the standard library, using method and path patterns in the router.
- Handle and wrap errors — Return errors, wrapping them with context so callers can inspect the cause with errors.Is and errors.As.
- Run goroutines and wait — Start work concurrently and wait for all of it to finish.
- Set a timeout with context — Cancel an operation if it takes longer than five seconds.
- Table-driven test — Run many cases through one test function, each as a named subtest.
Related cheatsheets
- C++ 23 cheatsheet — Compile and debug C++ with g++ and CMake, and use modern C++ (up to C++23): containers, algorithms, smart pointers, templates and threads.
- C# 14 cheatsheet — Modern C# 14 syntax for strings, nullability, collections, LINQ, records, pattern matching, async code and generics.
- CSS 3 cheatsheet — Layout with flexbox and grid, responsive design, custom properties, animations and modern selectors.
- HTML 5 cheatsheet — HTML5 page structure, text, links, images, forms, tables and metadata, with accessible patterns.
- Java, Maven and Gradle 25 cheatsheet — JDK commands to compile, run and diagnose Java programs, plus the Maven and Gradle commands for builds and tests.
- JavaScript 2025 cheatsheet — Modern JavaScript for arrays, strings, objects, async code, the DOM and modules, in copy-ready snippets.