
Staticcheck is a tool that helps you write better Go code by catching errors, formatting your code, and even suggesting improvements. It's a must-have for any serious Go developer.
Staticcheck can be installed using the go install command. You can also use the -v flag to see the version of staticcheck being installed.
Staticcheck's configuration is stored in a file named .staticcheck.yml. This file allows you to customize the tool's behavior and specify which rules to apply.
To get started with staticcheck, you'll need to run the staticcheck command in your terminal.
Recommended read: Golang Watcher
Installation
To install Staticcheck, you can use the command `go get` with the explicit version `v0.3.3`. This ensures the guide remains reproducible.
First, you need to install Staticcheck using `go get`. Note that you can use the `latest` version if you prefer.
To run Staticcheck, make sure it's on your PATH. You can do this by running it as a quick check.
Check that Staticcheck is installed correctly by running it as a quick check.
The Staticcheck website lists and documents all the categories and checks, including examples. You can also use the `-explain` flag to get details at the command line.
To install the latest version of Go, set the `install-go` option to `true`. However, if your job already installs Go prior to running Staticcheck, set this option to `false`.
Configuration
Staticcheck comes with some sensible defaults, but you can customize it with configuration files named staticcheck.conf that contain TOML.
You can inherit from the defaults by enabling specific checks, such as check ST1000, which covers missing package comments.
Creating a Staticcheck configuration file allows you to customize the tool to your needs, and re-running Staticcheck will verify that the new check is reported.
Configuring
Staticcheck works out of the box with some sensible, battle-tested defaults.
Staticcheck configuration files are named staticcheck.conf and contain TOML.

You can create a Staticcheck configuration file to enable specific checks, such as check ST1000, which covers cases like missing package comments.
Check ST1000 is not enabled by default, but can be enabled through a custom configuration file.
Staticcheck configuration files inherit from the Staticcheck defaults, allowing you to build on top of existing settings.
By re-running Staticcheck after creating a custom configuration file, you can verify that the enabled check is reported and confirm that there are no further problems.
Go Vet
Go Vet is a static code checker that's part of the standard Go installation. It's designed to catch code issues that are likely bugs.
Go Vet examines Go source code and reports suspicious constructs, such as Printf calls whose arguments don't align with the format string. It uses heuristics that don't guarantee all reports are genuine problems, but it can find errors not caught by the compilers.
One of the checks it runs is called unusedresult, which checks for unused results of calls to some functions. This sounds exactly like what we're looking for.

However, the unusedresult check requires you to list every function whose results must be used, which can be cumbersome. This is because the developers rejected a proposal to add this check to Go Vet, citing too many false positives.
Here are some common functions that can cause issues with the unusedresult check:
- fmt.Println: returns an error, but only in certain uncommon conditions
- Other functions in the Golang standard library: may return errors, but usually return nil
Go Vet won't protect us against these types of errors, but there are other tools that can help.
Version
The version of Staticcheck you use can significantly impact your build. It's recommended to pin to a specific version and update consciously.
New versions of Staticcheck introduce new checks that may break your build, so it's best to be mindful of updates. The default version is latest, which installs the latest released version of Staticcheck.
If you want to use a specific version, you can do so by specifying it in your configuration. This can help you maintain consistency and avoid unexpected changes.
Here's an interesting read: Golang Build
Usage
To use staticcheck, add it as a step in your existing workflow. This can be as simple as adding a single line to your workflow file.
You can integrate staticcheck with your existing tools, such as golangci-lint. For example, you can run staticcheck using the golangci-lint tool by running a command in the root of your Go project.
The command to run staticcheck with golangci-lint is quite straightforward: `golangci-lint run`. This will turn off all default linters and enable only the staticcheck linter.
For another approach, see: Golang Lint
Use Cache
Using the cache feature can significantly speed up your workflow with Staticcheck.
The default setting for the cache is true, which means it will automatically save and restore the cache using the actions/cache action.
Having the cache set to true allows you to work more efficiently, as you won't have to manually save and restore the cache each time you make changes.
Usage
To use staticcheck, you can add it as a step in your existing workflow. This is as simple as adding dominikh/staticcheck-action as a step in your workflow.

You can also create a more advanced workflow that runs tests, go vet, and Staticcheck on multiple OSs and Go versions. This might look similar to the example provided.
Adding staticcheck to your workflow can be done by following the documentation on GitHub's Actions. This will give you a comprehensive guide on how to write and tweak your workflows.
By integrating staticcheck into your workflow, you can catch errors and improve the quality of your code.
Merge Files
Merging files is a straightforward process. You can pass a newline-separated list of files to staticcheck -merge.
To merge files, you'll need to provide a list of files to merge. This list should be in a newline-separated format.
You can merge multiple files at once, which can save you time and effort. Just make sure to separate each file with a newline.
For example, if you want to merge two files, you would list them one after the other with a newline in between.
Build Tags

Build Tags are a powerful tool that allow you to pass flags to Staticcheck.
You can use Go build tags to get passed to Staticcheck via the -tags flag, which is a feature that helps you catch errors early in the development process.
Staticcheck is a tool that helps you catch errors in your Go code, and by using build tags, you can customize the checks that are run on your code.
Build tags are especially useful when working on large projects with multiple dependencies, as they allow you to selectively enable or disable checks for specific parts of your codebase.
To get the most out of build tags, you should experiment with different combinations of tags to see what works best for your project.
GoLint
GoLint is a single binary that comes bundled with all of the most useful Golang static analyzers. It can be run from the command line, but it also has its own GitHub Action, and plugins for every major IDE.
You can run GoLint from your IDE, and it's incredibly useful for catching bugs that unit or integration tests might miss. The author of the article actually found a bug in Dolt using GoLint that they wouldn't have caught otherwise.
Some of GoLint's checks can be noisy and less useful, but you can individually disable or adjust their severity. This is a big plus, as it means you can customize GoLint to your project's needs.
GoLint uses the Go Analysis API, which allows any new static analysis tool to have its checks incorporated into GoLint. This means you can configure every check from a single interface, making it super convenient.
Go Analysis API
The Go Analysis API is a powerful tool that allows static analysis tools to work together seamlessly. It provides official interfaces that analysis tools can implement to configure analysis passes, establish dependencies between them, and report results.
One of the coolest things about the Go Analysis API is that it enables tools like go vet, Staticcheck, and errcheck to work together in harmony. These tools all support the API, which means you can run them together in a single action with a single configuration.

Here are some popular tools that support the Go Analysis API:
- go vet
- Staticcheck
- errcheck
This API is so flexible that someone even created a single driver program called golangci-lint, which includes all these tools and runs them together in one place. That's a testament to the power of the Go Analysis API!
Checks and Issues
StaticCheck boasts an impressive 150 different checks, including one that warns about discarding the return values of a function without side effects.
StaticCheck's checks can be specified using the -checks flag, which normally doesn't need to be set and defaults to "inherit".
If you do need to specify checks, know that StaticCheck has a check for unused error return values, a feature that has been requested by users multiple times due to common practices in Golang code.
Fix Issues
Fixing issues is a crucial part of the development process. You can identify and resolve problems like SA4005, where a value is assigned to a variable but never used.

Removing unnecessary code is a simple fix for this type of issue. This helps keep your code clean and efficient.
Identifying and addressing potential bugs is key to creating a stable and reliable application. By removing unused code, you can prevent errors and improve overall performance.
In some cases, fixing one issue can reveal others that need attention. Regularly reviewing and updating your code can help you stay on top of these issues.
Checks
StaticCheck has an impressive 150 different checks, including one that flags unnecessary function calls with no side effects.
The -checks flag in StaticCheck allows you to specify which checks to run, but it defaults to "inherit" and doesn't normally need to be set.
StaticCheck's SA4017 check flags unnecessary function calls with no side effects, making the call pointless.
Errcheck is a small third-party tool that's been mentioned as a solution for checking unused error return values.
Deprecated Function/Element Used
Using a deprecated function or element can be a common issue in codebases. SA1019 is the staticcheck rule that flags these deprecations.
The first thing to note is that not all deprecations can be fixed immediately. In the case of Go library depreciation issues, backward compatibility is a strong commitment.
However, some deprecations may be internal to the app and must be maintained for backward compatibility until they can be removed with the next major release. This can be a significant challenge.
One approach to dealing with these deprecations is to suppress them temporarily by creating a custom configuration file. This can help avoid a lot of busy work.
For example, you can use the default staticcheck checks and turn off the SA1019 check. This can help reduce the number of fails and make it easier to focus on other issues.
Sa1032 - Wrong Error Args Order
The SA1032 error is a common issue that can sneak up on even the most seasoned developers. This error occurs when the order of arguments to the errors.Is function is wrong.
The error can be triggered by a simple mistake, like swapping the arguments. For example, the errors.Is function expects the error to be checked first, followed by the value to be checked.
Rerunning the linter can help catch this error, as seen in the example where a SA1032 failure was identified after re-running the linter.
Argument overwritten before first use
Argument overwritten before first use can be a challenging issue to fix, requiring specific code knowledge.
This type of bug occurs when a function argument is passed in but then immediately overwritten and never used, as seen in the SA4009 bug.
It's essential to identify this issue early on to avoid wasting time on code that won't be utilized.
Overwriting arguments can lead to unexpected behavior and make debugging more difficult.
In the case of SA4009, the function argument is passed in but then overwritten, making it a potential bug that needs to be addressed.
Output and Files
Staticcheck is a tool that helps you catch errors and improve the quality of your Go code. It's designed to be a linter, but one that's much more powerful than a traditional linter.
Staticcheck can analyze your Go code and output a report detailing any issues it finds. This report can be in the form of a JSON file, which can be easily parsed and used in your build process.
One of the key features of staticcheck is its ability to analyze your code and provide recommendations for improvement. It can suggest changes to your code that will make it more idiomatic and easier to maintain.
The output of staticcheck can be customized to fit your specific needs. You can specify which checks to run and which checks to ignore, allowing you to tailor the output to your project's requirements.
Prerequisites
To get started with Staticcheck, you should already have a solid grasp of Go fundamentals. This is a prerequisite for using Staticcheck effectively.
Staticcheck checks fall into different categories, each identified by a unique code prefix. Here are a few examples:
- Go fundamentals
You should already have completed Go fundamentals before moving on to Staticcheck. This will help you understand the nuances of Staticcheck and how it can improve your code.
Install Go
Installing Go is a straightforward process, but there are some considerations to keep in mind.
Whether you should install the latest version of Go depends on your job's specific requirements. If Staticcheck is the only action in your job, you can usually leave the install-go option on its default value of true.
If your job already installs Go prior to running Staticcheck, it's best to set this option to false. This way, you avoid installing Go unnecessarily and save time.
The latest release of Staticcheck works with the last minor release of Go at the time.
Prerequisites
To start with Staticcheck, you should already have a solid grasp of Go fundamentals. This is a crucial step in getting the most out of Staticcheck.
You'll need to have the latest release of Go installed on your system to run Staticcheck effectively. This is because Staticcheck can only analyze code targeting versions of Go up to the latest release.

Staticcheck can be compiled and run with the latest release of Go, so make sure your system is up to date.
The latest release of Staticcheck works with the last minor release of Go at the time, so you don't need to worry about compatibility issues.
Here are some key things to keep in mind:
- Go fundamentals are a prerequisite for using Staticcheck.
- Staticcheck requires the latest release of Go to run effectively.
- Staticcheck can analyze code targeting any version of Go up to the latest release.
- The latest release of Staticcheck works with the last minor release of Go at the time.
Tools and Libraries
The tools and libraries behind Go staticcheck are just as impressive as the tool itself. All of the tools can be found in the cmd/ directory, each accompanied by its own README file.
The tools available include staticcheck, which performs Go static analysis, detecting bugs, performance issues, and more. structlayout displays the layout of structs, showing field sizes and padding. structlayout-optimize reorders struct fields to minimize padding, while structlayout-pretty formats the output with ASCII art.
Here's a quick rundown of the tools:
The libraries behind these tools are also worth noting, as they aid in the implementation of the tools and are necessary for their functionality. However, be aware that none of these libraries have stable APIs, so you may encounter backwards-incompatible changes.
Tools

The tools available in the cmd/ directory are a treasure trove of productivity. They're all accompanied by their own README files, which provide more detailed information about each tool.
You can find the staticcheck tool in the cmd/ directory, which is a Go static analysis tool that detects bugs, performance issues, and more. It's a game-changer for catching errors early on.
The structlayout tool is another useful tool that displays the layout of structs, including field sizes and padding. This information can be super helpful when optimizing performance.
If you want to minimize padding in your structs, you can use the structlayout-optimize tool. It reorders struct fields to reduce the amount of padding.
For a more visual representation of struct layouts, you can use the structlayout-pretty tool, which formats the output with ASCII art. It's a great way to get a quick visual understanding of your struct layouts.
Here's a list of the tools available in the cmd/ directory:
Libraries

The libraries included in this repository are designed to aid in the implementation of the tools, but keep in mind that their APIs are not stable.
You'll have to expect semiregular backwards-incompatible changes if you decide to use these libraries, so it's essential to stay up-to-date with the latest developments.
These libraries are meant to be used as a foundation for the tools, but don't rely on them for long-term stability.
Their main purpose is to provide a starting point for building and customizing the tools, not to be used as a final solution.
Documentation
You can find extensive documentation on Staticcheck on its website. This is a great resource to learn more about how to use Staticcheck effectively.
The documentation is extensive, so you're sure to find what you need. Whether you're a beginner or an experienced developer, Staticcheck's documentation has got you covered.
Staticcheck's website is a one-stop shop for all your documentation needs. You can access it by following the link provided in the documentation section.
On a similar theme: Golang Find
Frequently Asked Questions
Is Golang relevant in 2025?
Yes, Golang remains relevant in 2025, offering simplicity, speed, and concurrency capabilities that continue to benefit large-scale systems. Its enduring qualities make it a valuable choice for building and maintaining backend and cloud-native applications.
Featured Images: pexels.com

