Not sure how to structure your Go web application?
My new book guides you through the start-to-finish build of a real world web application in Go — covering topics like how to structure your code, manage dependencies, create dynamic database-driven pages, and how to authenticate and authorize users securely.
Black Friday sale: 30% off until the 1st December!
Take a look!
Way back in 2019, I wrote the blog post An Overview of Go's Tooling. It was a popular article and IIRC, ended up being one of the most-read items in the Go Weekly newsletter that year. It's also one that I personally still come back to fairly often, when I want to remind myself of a specific command that I can't remember.
But a lot has changed in the past 7 years, and some of the information in that original post is now painfully out-of-date. So this is a version 2 of that post... fully updated for the module-enabled, gopls-integrated, token-fuelled world that we now live in 😊
- Installing and using third-party tools
- Development
- Using Gopls
- Viewing documentation and environment information
- Tidying and formatting code
- Tidying and formatting modfiles
- Debugging code
- Modernizing code
- Refactoring code
- Live reloading
- AI coding assistants and MCP
- Code quality
- Security
- Code correctness
- Code hygiene
- Running multiple tools
- Dependency management
- Managing Go versions
- Checking for dependency updates
- Using local dependencies and workspaces
- Tracing and visualizing dependencies
- Clearing downloaded dependencies
- Testing
- Running tests
- Profiling test coverage
- Load testing
- Stress testing
- Fuzz testing
- Building and working with binaries
- Verifying dependencies
- Building a binary
- Cross-compilation
- Compiler and linker flags
- Stripping debug information and reducing binary size
- Inspecting binaries and reproducible builds
- Diagnosing problems and making optimizations
- Running and analyzing benchmarks
- CPU, memory and goroutine profiling
- Profile-guided optimization
- Execution tracing
- Race detection
- Reporting bugs
Installing and using third-party tools
One of the biggest improvements to Go's tooling has been the introduction of new ways to run third-party tools. We
now have three options — go install, go run, and go tool — each
suited to slightly different use cases.
With go install
The first option is to install the tool on your local machine using the go install command. You can run
this command from anywhere on your machine, and it will download the necessary source code for the tool and create an
executable binary in your GOBIN directory. So long as your GOBIN directory is on your
system path, you can then run the tool whenever you need it.
GOBIN directory is, you can check its location by
running the go env GOBIN command.
For example, to install the latest version of govulncheck, check its install location, and run it, you
can do this:
$ go install golang.org/x/vuln/cmd/govulncheck@latest
$ which govulncheck
/home/alex/go/bin/govulncheck
$ govulncheck ./...
Using go install tends to be a good option for general-purpose tools that happen to be written in Go,
where you don't need to pin a specific version for use within a project or necessarily keep up with the latest
release. Personally, I tend to use it for installing tools like
hey, which I often use for things
outside of Go development.
With go run
The second option is to use go run to execute a specific version of a tool without installing it. For
example, to run the latest version of govulncheck you can use the following command:
$ go run golang.org/x/vuln/cmd/govulncheck@latest ./...
I've written in detail about using go run to execute tools previously in
this post, but this option
is really useful when there is a tool you want to quickly run on an ad hoc basis.
Personally, I often use go run in this way during code reviews, when I want to do a one-time run of a
specific
static analysis tool using the latest version.
I also find this approach useful in small solo projects where I don't have to worry about tool version differences
with other developers. I'll often create a Makefile which uses go run to execute the latest
version of code-quality tools —
similar to this.
With go tool
Lastly, there is now the option to add a tool as a module tool dependency to your go.mod file.
At a very high level, you can use the go get -tool command to add the tool as a module dependency, and
the necessary version information and checksums will be added to the go.mod and go.sum
files. For example:
$ go get -tool golang.org/x/vuln/cmd/govulncheck@latest
$ cat go.mod
module foo.example.com
go 1.27.3
tool golang.org/x/vuln/cmd/govulncheck
require (
golang.org/x/mod v0.42.0 // indirect
golang.org/x/sync v0.24.0 // indirect
golang.org/x/sys v0.49.0 // indirect
golang.org/x/telemetry v0.0.0-20261010193011-f71dc92da45e // indirect
golang.org/x/tools v0.52.0 // indirect
golang.org/x/vuln v1.9.0 // indirect
)
And then you can run the specific version of the tool listed in the go.mod file with the
go tool command, like so:
$ go tool govulncheck ./...
This is a great option when you are working with a team of people, where for consistency and reproducibility you want to make sure that everyone is using exactly the same version of a tool.
I've previously written a much more detailed tutorial about how to use the go tool functionality, and I
recommend
checking that out
for more information.
Development
Using Gopls
Another major development in recent years has been gopls. This is the official language server for Go, which you can use with any text editor or IDE that supports the Language Server Protocol (LSP).
If you're not already familiar with LSP, it's essentially a protocol that allows text editors to communicate with external programs called language servers, which act as the "brain" behind smart editor features.
The Go language server supports a wide range of smart editor features, including:
- Autocompletion
- Syntax highlighting
- Documentation on hover
- Comment and function collapsing
- Diagnostic checks that highlight invalid or bad-practice code
- Code navigation tools — including jump to definition, finding references, and finding implementations of a type
-
Refactoring and formatting tools — including smart renaming,
importstatement organization, moving function parameter positions, and generating skeleton unit tests for a function
To use gopls, the first thing you need to do is install it on your computer using the
go install command, like so:
$ go install golang.org/x/tools/gopls@latest
Once gopls is installed, you then need to configure your editor to use it.
-
If you're using Zed or Helix, they should automatically detect
goplson your machine and start using it without any additional setup. -
If you're using VS Code, you'll need to install the official Go extension. To do this, open the Extensions Panel, search for
golang.go, and install this extension. After you restart and open a.gofile, you should see a lightning bolt next to the Go version in the status bar at the bottom of your editor. This indicates that the language server is running. -
For other editors, the steps are a little more complicated. Please see the official setup steps for Vim and Neovim, Emacs and Sublime Text.
Once your editor is configured, you should see smart editor features become available. For example, here's a
screenshot of documentation when hovering over the http.Error() function in VS Code.
Viewing documentation and environment information
The go doc tool remains the de facto way to view the documentation for standard library packages in your
terminal. You can use it to view the documentation for an entire package, or for a specific type, function or method.
For example:
$ go doc bytes # View simplified documentation for the bytes package
$ go doc -all bytes # View full documentation for the bytes package
$ go doc bytes.Buffer # View documentation for the bytes.Buffer type
$ go doc bytes.Replace # View documentation for the bytes.Replace function
You can also use the -src flag to view the relevant source code:
$ go doc -src bytes.Replace # View the source code for the bytes.Replace function
Since Go 1.25, you can also use the -http flag to start a documentation server and browse the
documentation locally in your web browser. For example, to view documentation for the bytes package in
your browser you can run the following command:
$ go doc -http bytes
doc: Documentation server listening on addr http://localhost:43267
To display information about your current Go operating environment, you can use the go env command. This
can be particularly useful if you're working on an unfamiliar machine.
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/alex/.cache/go-build"
GOEXE=""
GOFLAGS=""
...
If there are specific values that you're interested in, you can pass them as arguments to go env. For
example:
$ go env GOPATH GOOS GOARCH
/home/alex/go
linux
amd64
To show documentation for all go env variables and values you can run:
$ go help environment
Tidying and formatting code
If you have your text editor setup to use gopls, there is a good chance that your .go files
will automatically be formatted according to the Go conventions each time they are saved, as well as having their
import lines automatically added, removed and sorted as necessary.
If you're not using gopls, or you want to format files outside of your text editor (for example, as part
of a pre-commit hook or CI pipeline), you can use the gofmt tool to format them. For example:
$ gofmt -w -s -d foo.go # Format the foo.go file
$ gofmt -w -s -d . # Recursively format all files in the current directory and sub-directories
In these commands, the -w flag instructs the tool to rewrite files in place, the -s
instructs the tool to apply certain
simplifications to the code, and the
-d flag instructs the tool to output diffs of the changes. If you want to only display the names of
changed files, instead of diffs, you can swap this for the -l flag instead. Note that
gofmt works recursively, so if you pass it a directory like . or ./cmd/foo
it'll format all .go files within that directory tree.
Alternatively, you can run the go fmt command — which is essentially a wrapper around
gofmt -l -w — like so:
$ go fmt ./...
Often, as part of a quality audit check in a CI pipeline, I want to check that .go files are formatted
correctly, without actually changing anything. To do this, I usually run gofmt -l . and check that the
output from this command is empty. There's an example of this in the audit check in
this Makefile.
It's also worth mentioning the
goimports tool, which formats your
.go files in the same way as gofmt, but also updates your import lines —
adding, removing and sorting the lines as necessary. You can run it like so:
$ go run golang.org/x/tools/cmd/goimports@latest -w .
Tidying and formatting modfiles
You can use the go mod tidy command to automatically add missing dependencies to, and prune unused
dependencies from, your go.mod and go.sum modfiles based on the imports in your
.go files.
$ go mod tidy
It's a good idea to run this periodically to keep your modfiles synchronized with your code. Personally, I try to run this before each commit to make it easier to see exactly which code changes were responsible for adding or removing a dependency when looking through the version control history.
If you want to check if the modfiles are synchronized without actually changing them, you can use the
-diff flag like so:
$ go mod tidy -diff
This is a really useful command to use in audit checks and CI pipelines, because when you run it with
-diff it will print out any changes and then exit with a non-zero code. If no changes are
needed, it will exit with code 0.
It's also worth mentioning that running go mod tidy ensures your modfiles include the dependencies
needed across all possible build tag, OS and architecture combinations. Commands like go run,
go test and go build are more selective, and only require dependencies for the current build
configuration. This is why you might sometimes see additional lines appear in your modfiles unexpectedly when running
go mod tidy.
Debugging code
For debugging Go applications, the standard tool to use is Delve. It allows you to set breakpoints, step through your code line by line, and do things like inspecting the values of variables and viewing the current call stack.
You can install Delve by running:
go install github.com/go-delve/delve/cmd/dlv@latest
Nowadays, the easiest way to use Delve is generally via the debugger integration in your text editor. For example, in VS Code you set breakpoints by clicking in the gutter next to a line of code, start a debugging session by opening the 'Run and Debug' panel, and use the debugging toolbar to step through your application and inspect what's happening as it runs.
It's also possible to use Delve outside of a text editor and start an interactive debugging session in your terminal.
For example, assuming you installed Delve using the go install command above, you could start debugging
an application in the ./cmd/web directory by running:
$ dlv debug ./cmd/web
You can then use Delve's commands to set breakpoints, continue execution, step through your code, and inspect variables, in much the same way as in a text editor.
There is a nice introduction on how to use the terminal-based debugger in this post, and more information about debugging with Delve can be found in the official Delve documentation.
Modernizing code
Go 1.26 made major changes to the go fix command, updating it so that it now automatically 'modernizes'
your code to use the latest Go idioms and standard library APIs. When you run it, it will do things like replacing
interface{} with any, and rewrite code to use functions like
slices.Contains() rather than manual loops.
You can see a complete list of the analyzers that go fix supports, and what they do, by running
go tool fix help:
$ go tool fix help
...
Registered analyzers:
any replace interface{} with any
atomictypes replace basic types in sync/atomic calls with atomic types
buildtag check //go:build and // +build directives
embedlit simplify references to embedded fields in composite literals
errorsastype replace errors.As with errors.AsType[T]
forvar remove redundant re-declaration of loop variables
etc...
At its most basic, you can use go fix like this:
$ go fix ./...
However, it's important to be aware that go fix will silently rewrite your .go files in
place. If you want to know in advance what changes it will make without actually applying them you can use
the -diff flag like so:
$ go fix -diff ./...
Sometimes, when looking at the diff, you might decide that you want to make some, but not all, of the changes. For
example, there have been times when I've decided that my original code was clearer without the modernizations applied
by the embedlit and mapsloop analyzers. You can disable specific analyzers individually like
so:
$ go fix -diff -embedlit=false -mapsloop=false ./...
modernize
package that was, and still is, available under golang.org/x/tools. All the analyzers that
modernize supports are now available via go fix, meaning that using go fix is
now the easiest and recommended way to modernize your codebase.
Refactoring code
Since the release of gopls, the simplest, safest and most flexible way to refactor your code is by using
its code transformation features in your text editor. For example, you can inline function calls, extract
code into a function, rename identifiers, and move or remove parameters across your codebase, all in a few clicks.
For example, in VS Code you can use the 'Rename' and 'Refactor' options in the right-click menu to refactor code like so:
More information, including a description of all supported refactoring features, can be found in the official
gopls documentation.
However, if you don't want to use gopls, or are using a text editor that doesn't support it, you can
still use gofmt with the -r flag to implement a rewrite rule that can assist with
certain refactoring tasks. I've written more about this in my 2019 blog post on Go tooling
here.
Live reloading
There are a few live reloading tools available for Go, which you can use to automatically rebuild and restart an application whenever certain files change. These are especially useful when developing web services, because they let you make changes to your code or template markup and then check the results in a browser without having to manually restart the application each time.
The most mature and popular live reloading tool for Go is
air-verse/air. You can run it using settings in a TOML
configuration file , like so:
$ go run github.com/air-verse/air@latest -c .air.toml
Or you can configure it using command-line flags. For example, to watch all directories and rebuild the
./cmd/web application whenever a file with an extension in the set .go,
.tmpl, .css, .js or .sql changes, you could use the following
command:
$ go run github.com/air-verse/air@latest \
--build.cmd "go build -o=/tmp/bin/web ./cmd/web" \
--build.entrypoint "/tmp/bin/web" \
--build.delay "100" \
--build.exclude_dir "" \
--build.include_ext "go, tmpl, css, js, sql" \
--misc.clean_on_exit "true" \
--misc.startup_banner ""
Here's a video illustrating the air live reloading functionality when developing a web application:
AI coding assistants and MCP
Go's language server, gopls, includes an experimental MCP (Model Context Protocol) server that exposes
some of its functionality to AI coding assistants (such as Claude Code or OpenAI Codex). This effectively allows the
assistants to leverage the same "smart features" that your text editor has access to, rather than just relying on
reading your files.
Assuming that you have gopls installed, you can configure your AI coding assistant to use the
gopls MCP server by following the instructions in the
official Go MCP documentation.
You can also run the gopls mcp -instructions command to get guidance for using the MCP server, which you
can then provide to your coding assistant. For example:
$ gopls mcp -instructions > ./agents/instructions/gopls.md
Another MCP server worth mentioning is
mrjoshuak/godoc-mcp. This allows AI coding
assistants to look up Go package documentation directly, which can be more token-efficient than reading through
source files. You may wish to try this if you're using AI assistants heavily and want to reduce token usage.
Code quality
Security
In late 2022 the Go team released
govulncheck, which checks your
codebase against the Go vulnerability database to find known vulnerabilities in
any standard library packages or third-party dependencies that you're using.
Unlike many other vulnerability scanners, it only flags vulnerabilities that actually affect you... meaning that a vulnerability in a part of a package that your code doesn't use won't be flagged.
It's an excellent tool, and I recommend running it routinely before you deploy a new version of your application to production.
Long term, the plan is for govulncheck to become part of the main go command, but for now
you can run it like so:
$ go run golang.org/x/vuln/cmd/govulncheck@latest ./...
Another really helpful tool is gosec, a static analyzer
that looks for potential security problems in your code. You can run it like so:
$ go run github.com/securego/gosec/v2/cmd/gosec@latest ./...
In my experience it's not uncommon for gosec to flag issues which on further inspection are false
positives in the context of your specific codebase, or where you are deliberately using something insecure. When this
happens, you can annotate the specific code with a #nosec comment to instruct gosec to omit
it from future reports. For example:
// #nosec G404 -- deliberate use of a deterministic RNG
rng := rand.New(rand.NewPCG(442, 4231))
Code correctness
go vet
The venerable go vet command should still be your first port of call when it comes to static code
analysis. It runs a relatively small set of checks, but the checks have a high signal-to-noise ratio and if
go vet reports an issue, then it's very likely to be a problem you should address.
It's normally best to run go vet with its default settings, like so:
$ go vet ./...
You can see a complete list of the checks that go vet runs in the 'Registered analyzers' section of the
help documentation:
$ go tool vet help
...
Registered analyzers:
appends check for missing values after append
asmdecl report mismatches between assembly files and Go declarations
assign check for useless assignments
atomic check for common mistakes using the sync/atomic package
bools check for common mistakes involving boolean operators
buildtag check //go:build and // +build directives
cgocall detect some violations of the cgo pointer passing rules
composites check for unkeyed composite literals
etc...
And you can also disable specific analyzers if you don't want their checks to run. For example, to disable the
composites analyzer, which checks for unkeyed composite literals, you can run go vet like
so:
$ go vet -composites=false ./...
Staticcheck
The staticcheck tool is kind of like go vet on
steroids. It carries out around 160 different checks on your code — the vast majority of which are related to
code correctness and performance, although it also includes a handful of checks for for 'stylistic' issues like comment
formatting too.
The complete list of checks, along with their respective ID codes and descriptions, can be found here.
It's important to note that staticcheck does not run all checks by default — only a
subset of them. But you can configure exactly which checks are run using either the -checks
command-line flag or a config file. For example:
# Run with default checks
$ go run honnef.co/go/tools/cmd/staticcheck@latest ./...
# Run with all checks enabled (recommended for a first pass)
$ go run honnef.co/go/tools/cmd/staticcheck@latest -checks=all ./...
# Run with all checks enabled except ST1000 and ST1020
$ go run honnef.co/go/tools/cmd/staticcheck@latest -checks=all,-ST1000,-ST1020 ./...
Nilaway
The nilaway tool is really useful for finding
potential nil-pointer panics in your code. It's similar to the older
nilness tool in what it aims to do,
but is more comprehensive in its checks.
You can run it like so:
$ go run go.uber.org/nilaway/cmd/nilaway@latest ./...
If you're not worried about nil-pointer panics in your test code, and only want to check your production
code, you can also run it with the -exclude-test-files flag enabled. Like so:
$ go run go.uber.org/nilaway/cmd/nilaway@latest -exclude-test-files ./...
Errcheck
The errcheck tool has been around for a long time (14
years!), and it remains a great way to check for unhandled errors in your code.
You can run it with its default settings like so:
$ go run github.com/kisielk/errcheck@latest ./...
It's worth mentioning that, by default, errcheck only reports errors which are
completely unhandled. It doesn't report errors that are handled by ignoring them and assigning them to the
blank identifier _.
To include these errors in the report, you can run it with the -blank flag enabled, like so:
$ go run github.com/kisielk/errcheck@latest -blank ./...
It's worth noting that not every error always needs to be handled. For example, it's conventional to ignore
errors returned by http.ResponseWriter.Write(), or errors from fmt.Fprint() when writing to
standard streams. Or you might deliberately choose to ignore errors from nice-to-have non-critical cleanup
operations, where you decide that handling errors would add more complexity to your codebase than it's worth.
So it's possible to exclude specific functions from the errcheck report by creating an
exclude file and passing it via the
-exclude flag like so:
go run github.com/kisielk/errcheck@latest -exclude=errcheck.exclude.txt -blank ./...
Shadow
It's also worth mentioning the
shadow tool, which
identifies instances of variable shadowing in your code.
This tool was also built by the Go team and has been around for a very long time, but has never been included in the
default go vet analyzers or the go command because it is
notoriously noisy and reports a lot of instances of variable
shadowing that aren't actually problematic.
Nonetheless, it's still worth knowing that it exists. Personally, I do use shadow occasionally when I'm
doing a code review of a large codebase, and skim through the results to see if anything jumps out as looking
unexpected or out of place.
You can run shadow like so:
$ go run golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow@latest ./...
Code hygiene
Revive
The revive tool is a code linter, and is kind of the spiritual successor to the now frozen and
deprecated golang/lint package.
There is some overlap between revive and the staticcheck tool that we talked about earlier,
but revive focuses more on checking code style and conventions, whereas
staticcheck focuses more on finding bugs and performance issues.
Generally, revive is good to use on projects where you have multiple contributors and you want to
maintain common standards in terms of code and comment style.
You can run it with its default settings like so:
$ go run github.com/mgechev/revive@latest ./...
Normally though, I prefer to run it using the -formatter=stylish flag, which groups failures by file and
includes links to the specific documentation for each failure.
$ go run github.com/mgechev/revive@latest -formatter=stylish ./...
You can also disable and customize specific checks by creating a TOML
configuration file and passing it via the
-config flag, like so:
$ go run github.com/mgechev/revive@latest -config=revive.toml ./...
Deadcode
Deadcode is an excellent tool for finding functions in your
code that aren't being used, which you can potentially delete or comment out. Similar to
govulncheck, the deadcode tool is officially maintained by the Go team, but isn't
currently part of the go command.
staticcheck runs some checks for unused code, deadcode is the more
specialized and comprehensive tool for this specific job, and is what I'd recommend using.
You can run deadcode with the default settings like this:
$ go run golang.org/x/tools/cmd/deadcode@latest ./...
But it's important to note that by default, deadcode does not consider code in _test.go
files when checking if something is used or not. This means that functions which are only used by your tests can
incorrectly be reported as dead code. So to make sure test files are included in the check, I almost always run
deadcode with the -test flag enabled, like so:
$ go run golang.org/x/tools/cmd/deadcode@latest -test ./...
Another neat way to use deadcode is when you are looking at a function and can't figure out why it
exists, or you're struggling to fully trace where it is being called from.
If you run deadcode with the -whylive flag, you'll get a report showing the shortest path
from a main() function in your program to the code in question. For example, let's say you have the
module example.com, and you wanted to trace where the
internal/response.HTMLRenderer.Render() method is being called from, you can run a command like this:
$ go run golang.org/x/tools/cmd/deadcode@latest -whylive=example.com/internal/response.HTMLRenderer.Render -test ./...
example.com/cmd/web.main
static@L0026 --> example.com/cmd/web.run
static@L0107 --> example.com/cmd/web.application.serveHTTP
static@L0044 --> net/http.Server.ListenAndServe
static@L3485 --> net/http.Server.Serve
static@L3604 --> net/http.conn.serve
static@L2157 --> net/http.serverHandler.ServeHTTP
dynamic@L3436 --> net/http.HandlerFunc.ServeHTTP
dynamic@L2358 --> example.com/cmd/web.application.home
static@L0015 --> example.com/internal/response.HTMLRenderer.RenderBase
static@L0033 --> example.com/internal/response.HTMLRenderer.Render
Wrapcheck
If you've made the decision in your codebase to wrap errors before returning them to the caller (which is generally a
good idea to help with debugging), the wrapcheck tool helps you find any instances where you've
forgotten to do this.
You can run it with:
$ go run github.com/tomarrell/wrapcheck/v2/cmd/wrapcheck@v2 ./...
Running multiple tools
If you are running a lot of tools as part of your code quality checks, you might want to consider
Golangci-lint to help with this. It acts as a wrapper around
many of the tools we've talked about above, running them in parallel and outputting the results in a single
consistent format.
Alternatively, and slightly more simply, I often use a Makefile with a single audit task that runs the
tools I want, similar
to this.
Dependency management
Managing Go versions
Personally, when I want to upgrade to a new version of Go on my primary development machine, I have a simple Bash script that I run to download and install the new version. The script and instructions are available here if you'd like to take a similar approach. I then run it like:
$ go-update 1.27.3
Sometimes it's useful to have multiple versions of Go installed on your machine. For example, you might want to test your code against different Go releases, or try out a new version without replacing your existing one.
You can do this using the go install golang.org/dl/... command. For example, to install Go 1.26.0
alongside your existing Go installation, you can run:
$ go install golang.org/dl/go1.26.0@latest
$ go1.26.0 download
You can then use this alternative version by running the versioned executable instead of go. For
example:
$ go1.26.0 version
$ go1.26.0 test ./...
$ go1.26.0 build ./cmd/web
It's important to note, though, that Go 1.21 and newer support automatic toolchain switching. So running a versioned
Go command inside a module that requires a newer toolchain may cause Go to
automatically download and use that newer version instead. If you want
to disable this, you can set the GOTOOLCHAIN environment variable to local, like so:
$ GOTOOLCHAIN=local go1.26.0 version
Checking for dependency updates
To check for dependency updates, the best tool around is probably
oligot/go-mod-upgrade. You can run it like so:
$ go run github.com/oligot/go-mod-upgrade@latest ./...
This launches an interactive tool which lists all dependencies that have upgrades available. You can then select the ones you want, and they'll be upgraded to the latest available version.
Using local dependencies and workspaces
In the past, if you want to use a modified version of a dependency (for example, you temporarily want to use a local
fork until a patch is merged upstream), you would use the go mod edit command to add a
replace rule to your go.mod file pointing to the modified version, as
described here.
Although this approach does still work, an alternative option is to create a
Go workspace and a go.work file in your module
root.
For example, assuming that you are in the root of your module, and the modified dependency you want to use is located
in the directory /local/code/mydep, you can create a workspace and a go.work file that
references the current module and modified dependency like so:
$ go work init .
$ go work use /local/code/mydep
The go.work file that is created should then look like this:
go 1.27.3
use (
.
/local/code/mydep
)
And when you run the code in your module, the modified dependency should be picked up and used, rather than
resolving the dependency from the module cache in the normal way. Note that the dependency still needs its own
go.mod file, and that the dependency's module path must match the import path your code uses.
Go workspaces are also really useful in the scenario where you are developing two modules at the same time which rely
on each other. When you want to do this, you can create a go.work file in the parent directory
of the two modules, which points to the two module directories. For example, if you have two modules located at
/local/code/a and /local/code/b, you could create a go.work file in
/local/code that references them both like so:
$ cd /local/code
$ go work init ./a ./b
For more information about Go workspaces and go.work files, please see the
official tutorial.
Tracing and visualizing dependencies
If you ever look at your go.mod or go.sum files and wonder "why is that listed as
a dependency?", you can find out by running the go mod why command. This will show you the shortest path
from a package in your main module to a given dependency. For example, to see why golang.org/x/sys is
listed as a dependency you can run:
$ go mod why -m golang.org/x/sys
go mod why command will return an answer for most, but not all,
dependencies. This is a long-standing issue, which is still open and being
tracked here.
It's also worth knowing about the go mod graph command, which prints a plain-text module requirement
graph.
$ go mod graph
The output from this tool is just a plain text listing, not a visual graph, but I've had some success with dumping
the output from go mod graph into AI tools and asking them to generate a visualization. Alternatively, if
you want something more reliable and reproducible, you might want to consider trying the
samber/go-mod-graph and
bayraktugrul/modview tools, which will generate
visualizations for you.
Clearing downloaded dependencies
Downloaded dependencies are stored in the module cache on your computer. If you're not sure where your
module cache is located, you can check by running the go env GOMODCACHE command.
If you ever want to clear the module cache — which can be useful if your cache has become corrupted or want to
rule it out as the source of a bug — you can use the go clean tool like so:
$ go clean -modcache
But be aware that this is a blunt instrument — it will remove the downloaded dependencies for all projects on your machine.
Testing
Running tests
You can use the go test tool to run the tests in your project like so:
$ go test ./... # Run all tests in the current directory and sub-directories
$ go test ./foo/bar # Run all tests in the ./foo/bar directory
If the code you’re testing leverages concurrency, or you’re running
tests in parallel, then you may want to
use the -race flag to enable Go's race detector
during the tests. This will increase the running time of the tests, but it's an easy tweak that can help find data
races that occur while the tests are running.
$ go test -race ./...
If any of your tests need access to VCS information in debug.BuildInfo, you should also make sure to use
the -buildvcs flag when running tests, like so:
$ go test -buildvcs ./...
If you want to only run some specific tests, rather than all the tests in a package, there are a few ways to do this.
If you're using VS Code or GoLand as your text editor, the simplest way is via the testing functionality built into your editor. For example:
If you're not using an editor that supports this kind of functionality, you can also use go test with
the -run flag to run specific tests. This accepts a regular expression, and only tests which have names
that match the regular expression will be run. When using this, I normally also use the -v flag to
enable verbose output, so I can make sure that the tests I expect are actually being run 🙂
$ go test -v -run=^TestFooBar$ . # Run the test with the exact name TestFooBar
$ go test -v -run=^TestFoo . # Run tests whose names start with TestFoo
$ go test -v -run=^TestFooBar$/^Baz$ . # Run the Baz subtest of the TestFooBar test only
It's also worth mentioning that Go caches test results at the package-level. If a package hasn't changed between test
runs, then the cached test result will be displayed with a "(cached)" annotation next to it. If you want
to force your tests to run in full, you can use the -count=1 flag, or mark cached test results as
expired by using the go clean tool.
$ go test -count=1 ./... # Bypass the test cache when running tests
$ go clean -testcache # Expire all cached test results
GOCACHE directory. Check go env GOCACHE if you're not sure where this is on your machine.
A final flag that I sometimes use is -failfast, which will stop further tests
in a package from running after the first failure.
$ go test -failfast ./...
Note that if you're running tests in multiple packages (for example by using go test ./... ), then the
tests in the other packages which didn't have the failure will continue to run.
As an alternative to go test, you may also want to consider using the
gotestyourself/gotestsum tool. This is
effectively a wrapper around go test which prints the test output in a nice way, similar to this:
Profiling test coverage
It's also possible to enable test coverage analysis when running tests by using the -cover flag. This
will display the percentage of code covered by the tests in the output for each package, similar to this:
$ go test -cover ./...
example.com/assets coverage: 0.0% of statements
ok example.com/cmd/web 0.027s coverage: 55.7% of statements
example.com/internal/assert coverage: 0.0% of statements
ok example.com/internal/database (cached) coverage: 90.0% of statements
etc...
Alternatively, you can use the -coverprofile flag to generate a coverage profile and view it in
your browser by using the go tool cover -html command like so:
$ go test -coverprofile=/tmp/profile.out ./...
$ go tool cover -html=/tmp/profile.out
This will give you a navigable listing of all the test files, with code covered by the tests displayed in green, and uncovered code in red.
If you want you can go a step further and set the -covermode=atomic flag to make the coverage profile
record the exact number of times that each statement is executed during the tests.
$ go test -covermode=atomic -coverprofile=/tmp/profile.out ./...
$ go tool cover -html=/tmp/profile.out
When viewed in the browser, statements which are executed more frequently are shown in a more saturated shade of green, similar to this:
If you don't have a web browser available to view a coverage profile, you can see a breakdown of test coverage by function/method in your terminal with the command:
$ go tool cover -func=/tmp/profile.out
example.com/assets/efs.go:17: sub 0.0%
example.com/cmd/web/errors.go:9: reportServerError 100.0%
example.com/cmd/web/errors.go:21: serverError 100.0%
example.com/cmd/web/errors.go:28: notFound 100.0%
example.com/cmd/web/errors.go:33: badRequest 100.0%
example.com/cmd/web/handlers.go:7: home 85.7%
etc...
Load testing
For quick and simple load testing of a web application or API, one of the best tools around is
rakyll/hey. For example, if you want to load test the
http://localhost:5051 endpoint by sending 10,000 requests with up to 100 concurrently, you can do so
like this:
$ go run github.com/rakyll/hey@latest -n 10000 -c 100 http://localhost:5051
The output includes useful information such as the number of requests per second, response-time distributions, and summary counts of HTTP response status codes.
hey supports a bunch of configuration options, which you can find by running:
$ go run github.com/rakyll/hey@latest -help
Alternatively, if you want a more sophisticated general-purpose load testing tool, you may want to consider
tsenart/vegeta or
grafana/k6, both of which are written in Go (although k6
test scenarios are typically written in JavaScript).
Stress testing
Occasionally you might want to stress test something in your application by running a test multiple times in succession. This can be useful for checking for sporadic or intermittent failures, or when you're trying to reproduce an intermittent bug that you've been seeing.
There are a couple of ways to do this, but the simplest is by using the go test flag
-count. For example, to run the TestFooBar test 500 times, you could run:
$ go test -run=^TestFooBar$ -count=500 ./...
It's important to note that -count repeats test executions in serial. Tests containing a
t.Parallel() instruction can still run concurrently with other parallel tests within each execution, but
the repeated executions themselves won't overlap.
If you want to repeat the same test multiple times in parallel, you can use the
stress tool.
To use the stress tool, you'll first need to compile a test binary for the specific package you want to
test. You can do this using the go test -c command. For example, to create a test binary for the
package in your current directory:
$ go test -c -o=/tmp/foo.test .
In this example, the test binary will be output to /tmp/foo.test. You can then use the
stress tool to execute a specific test in the test binary like so:
$ go run golang.org/x/tools/cmd/stress@latest -p=4 /tmp/foo.test -test.run=^TestFooBar$
60 runs so far, 0 failures
120 runs so far, 0 failures
...
Note: In the example above I've used the -p flag to restrict the number of parallel processes used by
stress to 4. Without this flag, the tool defaults to using a number of processes equal to
runtime.NumCPU().
Fuzz testing
Go 1.18 introduced support for fuzz testing, which makes it possible to run tests with randomly generated inputs in an attempt to find bugs and unhandled edge cases.
For example, let's say that you have a Slugify() function which takes input text like
"An example string" and turns it into a slug like "an-example-string".
func Slugify(s string) string {
s = strings.ToLower(s)
s = strings.TrimSpace(s)
s = strings.ReplaceAll(s, " ", "-")
return strings.Trim(s, "-")
}
You could create a FuzzSlugify fuzz test for it like so:
func FuzzSlugify(f *testing.F) {
f.Add("Hello World")
f.Add(" Hello World ")
f.Fuzz(func(t *testing.T, input string) {
slug := Slugify(input)
if strings.ToLower(slug) != slug {
t.Errorf("slug contains uppercase characters: %q", slug)
}
if strings.ContainsAny(slug, " \t\n\r") {
t.Errorf("slug contains whitespace: %q", slug)
}
if strings.HasPrefix(slug, "-") || strings.HasSuffix(slug, "-") {
t.Errorf("slug has leading or trailing hyphens: %q", slug)
}
})
}
In this example, the calls to f.Add() provide some seed corpus values for the fuzz test, which
give Go a starting point for generating additional inputs. You don't need to provide seed values, but it's
generally a good idea to include a few representative examples.
When you run go test ./... in the normal way, the FuzzSlugify test will be run, but only
using the seed corpus values.
To actually trigger fuzzing, you need to run it with the -fuzz flag. This takes a regular expression
matching the name of the specific fuzz test you want to run. It's only possible to run one fuzz test at a time, and
if multiple fuzz tests match the regular expression you'll get an error saying
-fuzz matches more than one fuzz test.
You also need to pass in the path to the package that contains the fuzz test — you cannot use
./....
So, for example, to execute the FuzzSlugify fuzz test in the ./cmd/web package, you would
run the following command:
$ go test -fuzz=^FuzzSlugify$ ./cmd/web/
fuzz: elapsed: 0s, gathering baseline coverage: 0/2 completed
fuzz: elapsed: 0s, gathering baseline coverage: 2/2 completed, now fuzzing with 20 workers
fuzz: minimizing 2375-byte failing input file
fuzz: elapsed: 0s, minimizing
— FAIL: FuzzSlugify (0.32s)
— FAIL: FuzzSlugify (0.00s)
helpers_test.go:26: slug contains whitespace: "0\t0"
Failing input written to testdata/fuzz/FuzzSlugify/dde7a120ccdf11af
To re-run:
go test -run=FuzzSlugify/dde7a120ccdf11af
FAIL
exit status 1
FAIL example.com/cmd/web 0.348s
In this example, the fuzz test found a problem with the Slugify() function — specifically, when the
input text is
"0\t0", the whitespace \t character is not removed. It's also important to note that, in
this example,
a file has been created at
testdata/fuzz/FuzzSlugify/dde7a120ccdf11af containing the failing input. This input will be included in
the seed corpus every time you run go test in the future (even when not using the -fuzz
flag), so that the bug doesn't accidentally get reintroduced.
Fuzzing will continue indefinitely until either a failure is found or you terminate the run manually (e.g. with
Ctrl+C). If there are no problems found, you'll see output which looks similar to this:
$ go test -fuzz=^FuzzSlugify$ ./cmd/web/
fuzz: elapsed: 0s, gathering baseline coverage: 0/2 completed
fuzz: elapsed: 0s, gathering baseline coverage: 2/2 completed, now fuzzing with 20 workers
fuzz: elapsed: 3s, execs: 80963 (26982/sec), new interesting: 80 (total: 82)
fuzz: elapsed: 6s, execs: 162497 (27177/sec), new interesting: 93 (total: 95)
fuzz: elapsed: 9s, execs: 417612 (85031/sec), new interesting: 105 (total: 107)
fuzz: elapsed: 12s, execs: 643491 (75312/sec), new interesting: 117 (total: 119)
fuzz: elapsed: 15s, execs: 799499 (51996/sec), new interesting: 127 (total: 129)
fuzz: elapsed: 18s, execs: 947482 (49334/sec), new interesting: 133 (total: 135)
fuzz: elapsed: 21s, execs: 1198032 (83509/sec), new interesting: 136 (total: 138)
fuzz: elapsed: 24s, execs: 1443035 (81674/sec), new interesting: 138 (total: 140)
fuzz: elapsed: 27s, execs: 1712495 (89800/sec), new interesting: 138 (total: 140)
fuzz: elapsed: 30s, execs: 1866026 (51175/sec), new interesting: 138 (total: 140)
...
The "new interesting" count is the number of inputs discovered that increase the code coverage observed by the fuzzer. When the number of "new interesting" inputs starts to plateau, it can be a sign that you're not getting much additional coverage from continuing the test and you may want to terminate it.
Alternatively, you can set a fixed runtime for the fuzz test using the -fuzztime flag. For example, to
run it for 30 seconds:
$ go test -fuzz=^FuzzSlugify$ -fuzztime=30s ./cmd/web/
For more information, I'd recommend checking out the official Go fuzzing documentation and the getting started tutorial.
Building and working with binaries
Verifying dependencies
Before compiling your application, it's sensible to run go mod verify to check that the dependencies in
your module cache haven't been modified since they were downloaded.
$ go mod verify
I normally run go mod verify immediately before running go build, as part of an automated
build process in a CI workflow or Makefile task. This helps to make sure that builds are reproducible, and you're not
using an accidentally modified dependency during compilation.
Building a binary
To compile a main package and create an executable binary you can use the go build tool.
Typically I use it in conjunction with the -o flag, which lets you explicitly set the output directory
and name of the binary like so:
$ go build -o=/tmp/foo . # Compile the package in the current directory
$ go build -o=/tmp/foo ./cmd/foo # Compile the package in the ./cmd/foo directory
In these examples, go build will compile the specified package (and any dependent packages), then
invoke the linker to generate an executable binary, then output this to /tmp/foo.
It's important to note that the go build tool caches build output in the
build cache. This cached output will be reused
again in future builds where appropriate, which can significantly speed up the overall build time.
go env GOCACHE command.
Using the build cache comes with one important caveat — it does not detect changes to C libraries imported
with cgo. So if your code imports a C library via cgo and you've made changes to it since
the last build, you'll need to use the -a flag which forces all packages to be rebuilt. Alternatively,
you could use go clean to purge the cache:
$ go build -a -o=/tmp/foo . # Force all packages to be rebuilt
$ go clean -cache # Remove everything from the build cache
If you ever want detailed output explaining precisely what go build is doing behind the scenes, you can
use the -v flag to print package names as they are being compiled, or the -x flag to print
the detailed commands that are being run. You'll probably want to use these in conjunction with the -a
flag, because otherwise you might not see much output as Go reuses packages from the build cache rather than
recompiling them.
$ go build -a -v -o=/tmp/foo .
$ go build -a -x -o=/tmp/foo .
Finally, if you run go build on a non-main package, it will be compiled in a temporary
location and again, the result will be stored in the build cache. No executable binary will be produced.
Cross-compilation
By default, go build will output a binary suitable for use on your current operating system and
architecture. But it also supports cross-compilation, so you can generate a binary suitable for use on a different
machine. This is particularly useful if you're developing on one operating system and deploying on another.
You can specify the operating system and architecture that you want to create the binary for by setting the
GOOS and GOARCH environment variables respectively. For example:
$ GOOS=linux GOARCH=amd64 go build -o=/tmp/linux_amd64/foo .
$ GOOS=windows GOARCH=amd64 go build -o=/tmp/windows_amd64/foo.exe .
To see a list of all supported OS/architecture combinations you can run go tool dist list:
$ go tool dist list
aix/ppc64
android/386
android/amd64
android/arm
android/arm64
darwin/amd64
darwin/arm64
...
For more information about cross compilation, I recommend reading this post.
Compiler and linker flags
When building your executable, you can use the -gcflags and -ldflags flags to change the
behavior of the compiler and linker and see more information about what it's doing. You can see a complete list of
available compiler and linker flags by running:
$ go tool compile -help
$ go tool link -help
Probably the most well-known and commonly-used of these is the linker -X flag, which allows you to
"burn in" a (string) value to a specific variable in your application. For example, to burn in the value "bar" into
the main package's global foo variable you can use it like so:
$ go build -ldflags="-X main.foo=bar" -o=/tmp/foo .
For more information about the -X flag and some sample code see this Stack Overflow
question.
Stripping debug information and reducing binary size
It's possible to reduce the binary size — typically by around 25% — by instructing the linker to strip
symbol
tables and DWARF debugging information from the binary. We
can do this by using the -s linker flag, like so:
$ go build -ldflags="-s" -o=/tmp/foo .
It's not a Go-specific tool, but you can also use a tool like upx
to compress the binary size further if you want. Please see
this post for more
information.
Inspecting binaries and reproducible builds
Once you have generated a binary, you can use the go tool buildid command to inspect its build
ID. This
is an identifier embedded in the binary by Go, representing the inputs to the build (code, compiler
version, flags, etc.) and the resulting output. It acts as a "fingerprint" for the build, and is also used
internally by Go's build cache.
For example, to check the build ID for the binary at /tmp/foo, you can run:
$ go tool buildid /tmp/foo
AqJJLNyqVF8tulgSF6hn/5Z57H6liLLo9bxiUhKMK/KjPvXxGPeQThYtWAe5Mi/ZRCLmtA8Biq8J-6sJcZG
Looking at the build ID can be useful when checking for reproducible builds, as identical builds should produce the same ID. If two builds produce different IDs, it suggests that something about them is different.
For more information about Go build IDs and reproducibility, please see this post.
Another useful tool is the go version -m command, which lets you inspect a binary's build information —
including the Go version, the module it was built from, its dependencies and VCS information. To inspect the binary
at /tmp/foo, you can use it like so:
$ go version -m /tmp/foo
/tmp/foo: go1.27.3
path example.com/cmd/foo
mod example.com v0.0.0-20261122131528-b0ecdafc0219
dep github.com/golang-migrate/migrate/v4 v4.20.1 h1:2N/ToVTKrKl58ynBpgeVJ4In7VcLCjWTZtm4eP1LxhU=
dep github.com/jmoiron/sqlx v1.4.0 h1:1PLqN7S1UYp5t4SrVVnt4nUVNemrDAtxlulVe+Qgm3o=
dep github.com/lmittmann/tint v1.2.1 h1:zOadah+f+nSm3jZM044jOsqh0+vSfgG/d9Ota9a/TLE=
dep github.com/mattn/go-sqlite3 v1.14.53 h1:mnTM0owzUMrKuLo+0w51NUiB0ir2Fo7kFXlPt/l3BGs=
dep golang.org/x/text v0.44.0 h1:YItdp/mQNGnsmrgpcdA5yQ3bnRxCfo/Tj/iByGW9GGo=
build -buildmode=exe
build -compiler=gc
build CGO_ENABLED=1
build CGO_CFLAGS=
build CGO_CPPFLAGS=
build CGO_CXXFLAGS=
build CGO_LDFLAGS=
build GOARCH=amd64
build GOOS=linux
build GOAMD64=v1
build vcs=git
build vcs.revision=b0ecdafc0219ee86c7e09d4778a530e6f8efbcf0
build vcs.time=2026-11-22T13:15:28Z
build vcs.modified=false
Diagnosing problems and making optimizations
Running and analyzing benchmarks
A nice feature of Go is that it makes it easy to benchmark your code. If you're not familiar with the general process for writing benchmarks there's a good guide here.
To run benchmarks you need to use the go test tool, with the -bench flag set to a regular
expression that matches the benchmarks you want to execute. If you don't want to run regular non-benchmark tests,
you should omit them by using the -run=^$ flag. For example:
$ go test -bench=. ./... # Run all benchmarks and tests
$ go test -run=^$ -bench=. ./... # Run all benchmarks (and no tests)
$ go test -run=^$ -bench=^BenchmarkFoo$ ./... # Run only the BenchmarkFoo benchmark (and no tests)
I almost always run benchmarks using the -benchmem flag, which includes memory allocation statistics in
the output.
$ go test -run=^$ -bench=. -benchmem ./...
By default, each benchmark test will be run for a minimum of 1 second, once only. You can control this with the
-benchtime and -count flags:
$ go test -run=^$ -bench=. -benchtime=5s ./... # Run each benchmark test for at least 5 seconds
$ go test -run=^$ -bench=. -benchtime=500x ./... # Run each benchmark test for exactly 500 iterations
$ go test -run=^$ -bench=. -count=3 ./... # Repeat each benchmark test 3 times over
If the code that you're benchmarking uses concurrency, you can use the -cpu flag to see the performance
impact of changing your GOMAXPROCS value (essentially, the number of OS threads that can execute your Go
code simultaneously). For example, to run benchmarks with GOMAXPROCS set to 1, 4 and 8:
$ go test -run=^$ -bench=. -cpu=1,4,8 ./...
To help analyze the results of the benchmarks, you can use the
benchstat tool. This replaces the
older benchcmp tool, which is now
deprecated.
When you are using the -count flag to run benchmarks multiple times, you can use
benchstat to easily see the median value and confidence interval across all the runs. For
example:
$ go test -run=^$ -bench=. -benchmem -count=10 ./... > result.txt
$ go run golang.org/x/perf/cmd/benchstat@latest result.txt
goos: linux
goarch: amd64
pkg: example.com
cpu: 12th Gen Intel(R) Core(TM) i7-12700H
│ result.txt │
│ sec/op │
Marshal-20 445.0n ± 6%
│ result.txt │
│ B/op │
Marshal-20 120.0 ± 0%
│ result.txt │
│ allocs/op │
Marshal-20 4.000 ± 0%
You can also use it to compare benchmarks before and after a specific change. For example:
$ go test -run=^$ -bench=. -benchmem -count=10 ./... > before.txt
$ go test -run=^$ -bench=. -benchmem -count=10 ./... > after.txt
$ go run golang.org/x/perf/cmd/benchstat@latest before.txt after.txt
goos: linux
goarch: amd64
pkg: foo
cpu: 12th Gen Intel(R) Core(TM) i7-12700H
│ before.txt │ after.txt │
│ sec/op │ sec/op vs base │
Marshal-20 773.3n ± 5% 451.6n ± 3% -41.61% (p=0.002 n=10)
│ before.txt │ after.txt │
│ B/op │ B/op vs base │
Marshal-20 590.0 ± 0% 120.0 ± 0% -79.66% (p=0.002 n=10)
│ before.txt │ after.txt │
│ allocs/op │ allocs/op vs base │
Marshal-20 12.000 ± 0% 4.000 ± 0% -66.67% (p=0.002 n=10)
CPU, memory and goroutine profiling
Go makes it possible to create diagnostic profiles for CPU use, memory use, goroutine blocking and mutex contention. You can use these to dig a bit deeper and see exactly how your application is using (or waiting on) resources.
There are three ways to generate profiles:
-
If you have a web application you can import the
net/http/pprofpackage. This registers handlers withhttp.DefaultServeMux, which you can then use to access profiling data from your running application. If you're using a different mux, you'll need to register the handlers with it yourself. This post provides a good explanation and some sample code. -
For other types of applications, you can profile your running application using the
pprof.StartCPUProfile()andpprof.WriteHeapProfile()functions. See theruntime/pprofdocumentation for sample code. -
Or you can generate profiles while running benchmarks or tests by using the various
-***profileflags. For example:
$ go test -run=^$ -bench=^BenchmarkFoo$ -cpuprofile=/tmp/cpuprofile.out .
$ go test -run=^$ -bench=^BenchmarkFoo$ -memprofile=/tmp/memprofile.out .
$ go test -run=^$ -bench=^BenchmarkFoo$ -blockprofile=/tmp/blockprofile.out .
$ go test -run=^$ -bench=^BenchmarkFoo$ -mutexprofile=/tmp/mutexprofile.out .
Using the -***profile flags when running benchmarks or tests will result in a test binary being
outputted to your current directory. If you want to output this to an alternative location you should use the
-o flag like so:
$ go test -run=^$ -bench=^BenchmarkFoo$ -o=/tmp/foo.test -cpuprofile=/tmp/cpuprofile.out .
My favourite way to inspect a profile is to use the go tool pprof -http command to open it in a web
browser. For example:
$ go tool pprof -http=:5000 /tmp/cpuprofile.out
This will default to displaying a flame graph visually showing where your application is spending its resources, with wide boxes being the 'hot spots'.
You can also navigate to other views of the profile including top usage by function and source code:
And also a graph showing the execution tree for your application, like this:
If the amount of information is overwhelming, for CPU profiles you might want to use the
-nodefraction flag to ignore nodes that account for less than a certain percentage of samples. For
example, to ignore nodes that appear in less than 10% of samples you can run pprof like so:
$ go tool pprof --nodefraction=0.1 -http=:5000 /tmp/cpuprofile.out
And finally, if you're analyzing profiles for a web application created via net/http/pprof, you can also
fetch the
profile from the application and open the visualization in one step. For example, if your web application is running
on http://localhost:4000 you could generate a 30-second CPU profile and view it like so:
$ go tool pprof -http=:5000 http://localhost:4000/debug/pprof/profile?seconds=30
For more information about profiling and pprof, please see the official
README.
Profile-guided optimization
Go also supports profile-guided optimization (PGO), which allows the compiler to use information gathered from profiling your application to make better decisions when compiling your code.
Normally, when compiling your code, the compiler has to make educated guesses about which functions are likely to be called frequently, which code paths are more common, and where it should make optimizations. PGO allows you to provide a CPU profile from a representative run of your application so that the compiler can make better-informed decisions.
For example, it may use this information to more aggressively inline frequently called functions, or optimize common code paths.
The good news is that using PGO is relatively straightforward. First you need to generate a CPU profile using one of
the
techniques described in the previous section. Then you need to pass it to go build via the
-pgo flag when building your application. For example, if your CPU profile is saved as
/tmp/cpuprofile.out, you can use it like so:
$ go build -pgo=/tmp/cpuprofile.out ./cmd/web
Alternatively, you can rename your profile to default.pgo and place it in the directory containing your
main package. Go will automatically detect this file and use it when building your application, so you
don't need to specify the -pgo flag explicitly.
In practice, the recommended workflow is to collect a representative profile from your production application, use that profile when building your next release, and periodically collect new profiles as your application evolves.
For more information, see the official PGO documentation and the Go blog post introducing PGO.
Execution tracing
Sometimes profiling isn't enough to understand what's happening in your application. For example, a CPU profile can show you which functions are consuming the most CPU time, but it won't necessarily help you understand why a goroutine is blocked, why your application isn't making progress, or why a request is taking so long.
For these situations, Go has a built-in execution tracer, which has been improved significantly in recent Go
versions. It records runtime events such as goroutine scheduling, system calls, garbage collection, and other
activity over a period of time. You can then use the go tool trace command to visualize and explore what
happened.
You can generate a trace from your tests or benchmarks, use net/http/pprof to create and download a
trace for your web application, or use the runtime/trace package to generate traces from a running
application. You can then use go tool trace to view the output in your web browser. For example:
$ go test -run=^$ -bench=^BenchmarkFoo$ -trace=/tmp/trace.out .
$ go tool trace /tmp/trace.out
This will open an interactive visualization that lets you explore what was happening during the trace.
For more information, please see the Go blog post on tracing,
the runtime/trace documentation, as well as this
dotGo conference talk by Rhys Hiltner.
Race detection
I talked earlier about enabling Go's race detector during tests by using go test -race. But you can
also enable it for running programs when building an executable, like so:
$ go build -race -o=/tmp/foo .
It's critical to note that race-detector-enabled binaries will use more CPU and memory than normal, so you shouldn't
use the -race flag when building binaries for production under normal circumstances.
But you may want to deploy a race-detector-enabled binary on one server within a pool of many. Or use it to help track down a suspected race condition by using a load-test tool to throw traffic concurrently at a race-detector-enabled binary.
By default, if any races are detected while the binary is running, a report will be written to os.Stderr.
You can
change this by using the GORACE
environment variable if necessary. For example, to run the binary located at /tmp/foo and output any race
reports to
/tmp/race.<pid> you can use:
$ GORACE="log_path=/tmp/race" /tmp/foo
Another complementary tool for helping to track down race conditions is
amit-davidson/Chronos. This uses static analysis
of your code to try to identify race conditions, rather than using real-time detection like Go's race detector. This
means it can be a good option to try if there is a suspected race condition that you are struggling to
replicate.
Reporting bugs
If you're confident that you've found an unreported issue with Go's standard library, tooling or documentation, you
can use the go bug command to create a new GitHub issue.
$ go bug
This will open a browser window containing a GitHub issue pre-filled with your system information and reporting template.
If you enjoyed this post...
You might like to check out my other Go tutorials on this site, or if you're after something more structured, my books Let's Go and Let's Go Further cover how to build complete, production-ready, web apps and APIS with Go.