建议使用发布版本而非 master 分支,master 可能含破坏构建的新检查或不兼容改动;仓库内库 API 不稳定,会有不兼容变更
go install honnef.co/go/tools/cmd/[email protected]README 未说明,仅要求用最新版 Go 编译和运行,可分析任意 Go 版本代码
Staticcheck 是 Go 语言的先进静态分析 linter,用于发现 bug、性能问题和风格问题
Staticcheck 是 Go 语言的进阶静态分析工具,能查出编译器发现不了的 bug、性能问题和可疑代码,帮你在上线前把隐患拦下来。
核心能力
- 用静态分析检测 Go 代码中的 bug 和性能问题
- 提供代码简化建议并强制风格规则
- 附带 structlayout 系列工具分析结构体内存布局
- structlayout-optimize 可重排字段以减少填充
- 仓库同时提供实现这些工具的库
容易踩的坑
- 2026.2.2Staticcheck 2026.2.2 (v0.8.2)最新正式版2026-10-12 · 2026-10-12 14:05 UTC
This release addresses the following false positives, crashes, and compatibility issues:
- SA4023 more accurately tracks the nilness of interface values across branches and when loading global variables, fixing further false positives introduced with 2026.2 (issue 1737, issue 1738). Furthermore, it no longer assumes that zero values of unconstrained type parameters are always nil.
- SA4023 no longer crashes when checking calls to functions from indirect dependencies (issue 1741).
- SA1019 no longer crashes on composite literals of generic structs that set deprecated fields (issue 1821).
- A crash when comparing values of type parameters with their zero values using ordering comparisons such as <= has been fixed.
- Staticcheck now supports the updated export data format used by Go 1.27.2.
- Several bugs in the intermediate representation involving generic methods, closures in methods of generic types, and nil unsafe.Pointer values have been fixed.
- The intermediate representation now correctly distinguishes goto from continue when jumping to a label on a range-over-func loop. Furthermore, it preserves the correct evaluation order for receive assignments in single-case select statements.
Furthermore, this release reduces memory usage and avoids unnecessary work when building the intermediate representation, particularly when handling methods and generic types.
- 2026.2.1Staticcheck 2026.2.1 (v0.8.1)2026-08-21 · 2026-08-21 20:43 UTC
查看更新说明
This release fixes two false positives in SA4023 that were introduced with 2026.2:
- It no longer treats unconstrained generics as never nil (#1736).
- It no longer triggers in situations that don't involve typed nils (#1737).
- 2026.2Staticcheck 2026.2 (v0.8.0)2026-08-20 · 2026-08-20 12:47 UTC
查看更新说明
Added support for Go 1.27
This release updates Staticcheck’s database of deprecated standard library APIs to cover Go 1.27. Furthermore, it adds support for two new language features: generic methods and struct initializers with direct references to embedded fields
Support for GOCACHEPROG
Staticcheck now supports the same
GOCACHEPROGenvironment variable and protocol as thegotool. This allows users to provide their own implementation of the build/fact/diagnostic cache.Case-insensitive check names
All places that accept names of checks, such as the
-checksand-explainflags, ignore directives, and thecheckssetting in configuration files now ignore the case of the provided names. That is,SA1000,sa1000, andsA1000all refer to the same check.Performance improvements for some packages
We have made changes to better support code with very complex control flow graphs, such as the output of wasm2go. In one particularly pathological case, memory usage was reduced from 10 GiB to 1.5 GiB and wall time from 73 s to 13 s.
Checks
New checks
One new check has been added. SA9010 flags statements of the form
defer foo()wherefooreturns a function. Usually, functions like that expect the user to defer calling the returned function.For example:
// Start starts a thing. The returned function has to be called once the thing // is no longer needed. func Start() func() { // do some work here return func() { // undo some work here } } func user() { // This is doubly wrong! It won't call Start until this function returns, // and then it won't call the function returned by Start. defer Start() ... }Changed checks
The following checks have been improved:
- SA1019 now flags uses of deprecated fields in struct initializers. Additionally, it is more consistent in the way diagnostics are formatted.
- SA1026 and SA9005 now check calls to
MarshalIdentfunctions, too. - SA4003 now checks comparisons involving generics.
- SA4006 has gained support for compound operators (
+=,^=, etc) as well as postfix increment and decrement. - SA4023 has been reimplemented. As a result, it may find new issues in existing code, but also miss issues it has found before.
- SA5011 has been disabled due to internal changes. It is unclear whether it will be enabled again in the future. As an alternative, gopls contains the nilness analyzer, which has similar checks.
- SA6005 emits a clearer diagnostic when the result of
strings.EqualFoldhas to be inverted. - Documentation has been added to several checks (SA1002, SA1012,
- 2026.2rc1Staticcheck 2026.2rc1 (v0.8.0-rc.1)pre-release2026-06-25 · 2026-06-25 17:57 UTC
查看更新说明
This is a release candidate of Staticcheck 2026.2. It has been built with Go 1.27rc1 and supports analyzing Go 1.27 code.
Added support for Go 1.27
This release updates Staticcheck’s database of deprecated standard library APIs to cover Go 1.27. Furthermore, it adds support for two new language features: generic methods and struct initializers with direct references to embedded fields
Support for GOCACHEPROG
Staticcheck now supports the same
GOCACHEPROGenvironment variable and protocol as thegotool. This allows users to provide their own implementation of the build/fact/diagnostic cache.Case-insensitive check names
All places that accept names of checks, such as the
-checksand-explainflags, ignore directives, and thecheckssetting in configuration files now ignore the case of the provided names. That is,SA1000,sa1000, andsA1000all refer to the same check.Performance improvements for some packages
We have made changes to better support code with very complex control flow graphs, such as the output of wasm2go. In one particularly pathological case, memory usage was reduced from 10 GiB to 1.5 GiB and wall time from 73 s to 13 s.
Checks
New checks
One new check has been added. SA9010 flags statements of the form
defer foo()wherefooreturns a function. Usually, functions like that expect the user to defer calling the returned function.For example:
// Start starts a thing. The returned function has to be called once the thing // is no longer needed. func Start() func() { // do some work here return func() { // undo some work here } } func user() { // This is doubly wrong! It won't call Start until this function returns, // and then it won't call the function returned by Start. defer Start() ... }Changed checks
The following checks have been improved:
- SA1019 now flags uses of deprecated fields in struct initializers. Additionally, it is more consistent in the way diagnostics are formatted.
- SA1026 and SA9005 now check calls to
MarshalIdentfunctions, too. - SA4003 now checks comparisons involving generics.
- SA4006 has gained support for compound operators (
+=,^=, etc) as well as postfix increment and decrement. - SA4023 has been reimplemented. As a result, it may find new issues in existing code, but also miss issues it has found before.
- SA5011 has been disabled due to internal changes. It is unclear whether it will be enabled again in the future. As an alternative, gopls contains the nilness analyzer, which has similar checks.
- SA6005 emits a clearer diagnostic when the result of
strings.EqualFoldhas to be inverted. - Documentation has been added to several checks (SA1002, [SA1012](https://sta
- 2026.1Staticcheck 2026.1 (v0.7.0)2026-02-13 · 2026-02-13 19:08 UTC
查看更新说明
Improved Go 1.25 and Go 1.26 support
This release updates Staticcheck’s database of deprecated standard library APIs to cover the Go 1.25 and Go 1.26 releases, as well as to add some
crypto/ellipticdeprecations from Go 1.21 that were missing. Furthermore, it adds support fornew(expr), which was added in Go 1.26.Other changes
- Version mismatch checks have been relaxed and no longer care about mismatches in the patch level. For example, Staticcheck built with Go 1.26.0 will be able to check code using Go 1.26.1.
- Staticcheck no longer opens
staticcheck.conffiles that aren’t regular files (or symlinks to regular files). See this gomodfs issue for the motivation behind this change. - Staticcheck now exits with a non-zero status code if it encountered an invalid configuration file.
Checks
Changed checks
The following checks have been improved:
- SA1026 no longer panics when checking code that tries to marshal named functions (issue 1660).
- SA4000 no longer flags
var _ = T{} == T{}, a pattern used to ensure that type T is comparable (issue 1670). - SA4000 now correctly skips structs containing floats.
- SA4000 now skips functions from the
math/rand/v2package. - SA4003 now skips over generated files.
- SA4030 now also checks uses of
math/rand/v2. - SA5008 has been updated with better support for
encoding/json/v2. - SA5010 no longer tries to reason about generics, to avoid false positives.
- ST1019 no longer flags duplicate imports of unsafe, mainly to play nice with cgo.
- ST1003 and QF1002 now emit more concise positions, benefitting users of gopls (issue 1647).
- ST1019 now allows importing the same package twice, once using a blank import (issue 1688).
- QF1008 no longer offers to delete all embedded fields from a selector expression. Even when two fields are individually superfluous, removing both might change the semantics of the code (issue 1682).
- QF1012 now detects more uses of
bytes.Buffer(issue 1097). - A bug in the intermediate representation was fixed, affecting the behavior of various checks (issue 1654).
用户点评
登录用户的真实使用记录,可按版本和硬件筛选还没有人点评。第一个说出真实体验的人最有价值。
Staticcheck is a state of the art linter for the Go programming language. Using static analysis, it finds bugs and performance issues, offers simplifications, and enforces style rules.
Financial support by private and corporate sponsors guarantees the tool's continued development. Please become a sponsor if you or your company rely on Staticcheck.
Documentation
You can find extensive documentation on Staticcheck on its website.
Installation
Releases
It is recommended that you run released versions of the tools.
These releases can be found as git tags (e.g. 2022.1).
The easiest way of installing a release is by using go install, for example go install honnef.co/go/tools/cmd/[email protected].
Alternatively, we also offer prebuilt binaries.
You can find more information about installation and releases in the documentation.
Master
You can also run the master branch instead of a release. Note that while the master branch is usually stable, it may still contain new checks or backwards incompatible changes that break your build. By using the master branch you agree to become a beta tester.
Tools
All of the following tools can be found in the cmd/ directory. Each tool is accompanied by its own README, describing it in more detail.
| Tool | Description |
|---|---|
| staticcheck | Go static analysis, detecting bugs, performance issues, and much more. |
| structlayout | Displays the layout (field sizes and padding) of structs. |
| structlayout-optimize | Reorders struct fields to minimize the amount of padding. |
| structlayout-pretty | Formats the output of structlayout with ASCII art. |
Libraries
In addition to the aforementioned tools, this repository contains the libraries necessary to implement these tools.
Unless otherwise noted, none of these libraries have stable APIs. Their main purpose is to aid the implementation of the tools. You'll have to expect semiregular backwards-incompatible changes if you decide to use these libraries.
System requirements
Staticcheck can be compiled and run with the latest release of Go. It can analyze code targeting any version of Go upto the latest release.