Top Related Projects
Fast linters runner for Go
🔥 ~6x faster, stricter, configurable, extensible, and beautiful drop-in replacement for golint
Go security checker
errcheck checks that you checked errors.
[mirror] This is a linter for Go source code. (deprecated)
Quick Overview
dominikh/go-tools is a collection of static analysis tools for Go programming language. It provides a set of linters and analyzers that help developers identify potential issues, improve code quality, and enforce best practices in Go projects.
Pros
- Comprehensive set of static analysis tools tailored for Go
- Regularly updated and maintained by the community
- Integrates well with popular IDEs and code editors
- Helps catch common programming errors and style issues early in development
Cons
- Some tools may produce false positives in certain scenarios
- Learning curve for understanding and configuring all available tools
- Can be resource-intensive when running on large codebases
- May require additional setup for CI/CD integration
Code Examples
- Using the
staticcheck
tool:
package main
import "fmt"
func main() {
x := 10
if x == 10 {
fmt.Println("x is 10")
} else if x == 10 { // staticcheck will flag this as an issue
fmt.Println("This will never be reached")
}
}
- Using the
gosimple
tool:
package main
import "strings"
func simplifyMe(s string) bool {
return strings.ToLower(s) == strings.ToLower("hello") // gosimple will suggest using strings.EqualFold
}
- Using the
unused
tool:
package main
import "fmt"
func main() {
x := 5 // unused will flag this as an unused variable
fmt.Println("Hello, World!")
}
Getting Started
To get started with dominikh/go-tools, follow these steps:
-
Install the tools:
go install honnef.co/go/tools/cmd/staticcheck@latest
-
Run the analysis on your Go project:
staticcheck ./...
-
Review the output and address any issues reported by the tools.
-
Optionally, integrate the tools with your IDE or CI/CD pipeline for continuous code quality checks.
Competitor Comparisons
Fast linters runner for Go
Pros of golangci-lint
- Integrates multiple linters into a single tool, providing a comprehensive analysis
- Offers faster execution through parallel processing and caching mechanisms
- Provides easy configuration and CI integration with sensible defaults
Cons of golangci-lint
- May have a steeper learning curve due to its extensive feature set
- Can potentially produce more false positives due to the large number of integrated linters
Code comparison
go-tools (staticcheck):
package main
func main() {
var x int
x = x
}
golangci-lint:
package main
func main() {
var x int
x = x // golangci-lint: self-assignment of x to x (gocritic)
}
Both tools would detect the self-assignment issue, but golangci-lint provides more detailed output by default, including the specific linter (gocritic) that caught the problem.
golangci-lint offers a more comprehensive solution for Go code analysis, integrating multiple linters and providing faster execution. However, go-tools (specifically staticcheck) is known for its high-quality, low false-positive rate analysis, making it a solid choice for projects that prioritize accuracy over breadth of checks.
🔥 ~6x faster, stricter, configurable, extensible, and beautiful drop-in replacement for golint
Pros of revive
- Highly configurable with a flexible rule system
- Faster execution time due to concurrent processing
- Provides a rich set of built-in rules and supports custom rules
Cons of revive
- Less comprehensive analysis compared to go-tools
- May require more setup and configuration for optimal use
- Fewer advanced static analysis features
Code Comparison
revive:
package main
func main() {
// revive:disable:errcheck
someFunction()
// revive:enable:errcheck
}
go-tools:
package main
func main() {
// staticcheck:ignore SA1019 deprecated function
someDeprecatedFunction()
}
Both tools allow for inline comments to disable specific checks, but they use different syntax and offer varying levels of granularity in their analysis.
revive focuses on linting and style checks, while go-tools (specifically the staticcheck tool) provides more in-depth static analysis, including advanced checks for potential bugs and inefficiencies.
revive is more suitable for teams looking for a highly customizable linter with quick execution times, while go-tools is better for projects requiring comprehensive static analysis and advanced bug detection capabilities.
Go security checker
Pros of gosec
- Focused specifically on security-related issues in Go code
- Integrates well with CI/CD pipelines and provides machine-readable output
- Regularly updated with new security checks and vulnerability patterns
Cons of gosec
- More limited scope compared to go-tools' broader static analysis capabilities
- May produce more false positives due to its security-focused approach
- Less extensive documentation and community support
Code Comparison
gosec example:
// gosec G104
_, err := os.Open("file")
if err != nil {
panic(err)
}
go-tools example:
// staticcheck SA1006
if x == nil {
return nil
}
return &x
gosec focuses on identifying potential security vulnerabilities, such as unchecked errors that could lead to unexpected behavior. go-tools, on the other hand, provides a broader range of static analysis checks, including code style and potential bugs.
Both tools are valuable for improving Go code quality, but they serve different purposes. gosec is ideal for projects prioritizing security audits, while go-tools offers a more comprehensive static analysis solution for general code quality improvement.
errcheck checks that you checked errors.
Pros of errcheck
- Focused specifically on error checking, making it more lightweight and specialized
- Simpler to use and configure for error checking tasks
- Faster execution time for error checking due to its specialized nature
Cons of errcheck
- Limited functionality compared to go-tools, which offers a broader range of analysis tools
- Less actively maintained, with fewer recent updates and contributions
- May miss some complex error handling scenarios that go-tools can detect
Code Comparison
errcheck:
if err := doSomething(); err != nil {
return err
}
go-tools:
if err := doSomething(); err != nil {
return fmt.Errorf("failed to do something: %w", err)
}
go-tools provides more comprehensive static analysis, including suggestions for better error wrapping and context preservation.
Summary
errcheck is a lightweight, focused tool for error checking in Go code. It's easy to use and fast but lacks the broader functionality of go-tools. go-tools offers a more comprehensive suite of analysis tools, including error checking, and is more actively maintained. While errcheck is suitable for quick error checking tasks, go-tools provides deeper insights and a wider range of code quality improvements.
[mirror] This is a linter for Go source code. (deprecated)
Pros of golang/lint
- Official Go project, maintained by the Go team
- Integrated into many Go development tools and IDEs
- Focuses on style and common coding errors
Cons of golang/lint
- Limited scope compared to go-tools
- Less frequent updates and new checks
- Doesn't include more advanced static analysis features
Code Comparison
golang/lint:
func (f *File) lintErrorf(pos token.Pos, format string, args ...interface{}) {
f.errors = append(f.errors, Error{pos, fmt.Sprintf(format, args...)})
}
go-tools:
func (pass *analysis.Pass) Reportf(pos token.Pos, format string, args ...interface{}) {
pass.Report(analysis.Diagnostic{
Pos: pos,
Message: fmt.Sprintf(format, args...),
})
}
Summary
golang/lint is the official Go linter, focusing on style and common errors. It's widely integrated but has a more limited scope. go-tools offers a broader range of static analysis tools and more frequent updates. The code comparison shows similar error reporting functions, but go-tools uses a more structured approach with the analysis package.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
The advanced Go linter
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/staticcheck@2022.1
.
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.
Top Related Projects
Fast linters runner for Go
🔥 ~6x faster, stricter, configurable, extensible, and beautiful drop-in replacement for golint
Go security checker
errcheck checks that you checked errors.
[mirror] This is a linter for Go source code. (deprecated)
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot