Staticcheck is a static analysis tool for Go programming language. It is designed to improve code quality by identifying bugs, performance issues, and other potential problems in Go code. This tutorial demonstrates how to install Staticcheck on Ubuntu 24.04.
Prepare environment
Make sure you have Go installed on your machine before proceeding. You can find the installation guide in another post.
Install Staticcheck
Download latest version of Staticcheck:
wget -qO staticcheck.tar.gz https://github.com/dominikh/go-tools/releases/latest/download/staticcheck_linux_amd64.tar.gz
Extract executable to /usr/local/bin
directory:
sudo tar xf staticcheck.tar.gz --strip-components=1 -C /usr/local/bin staticcheck/staticcheck
Here's how you can determine the version of Staticcheck:
staticcheck --version
Remove downloaded archive:
rm -rf staticcheck.tar.gz
Testing Staticcheck
Create the main.go
file:
nano main.go
Add the following code:
package main
import "fmt"
func main() {
fmt.Println(2 % 1)
}
To use Staticcheck, run the command staticcheck
followed by the name of the Go file you want to analyze, like:
staticcheck main.go
This will perform a static analysis, identifying potential bugs, and code improvements. Output:
main.go:6:17: x % 1 is always zero (SA4028)
Uninstall Staticcheck
Remove the relevant file to uninstall Staticcheck:
sudo rm -rf /usr/local/bin/staticcheck
Leave a Comment
Cancel reply