Golang - Write better code with Golang linters — Golangci-lint
Originally published on an external platform.
Linters are essential tools that help identify obvious errors and perform static code analysis to improve overall code quality. No matter how experienced you are, there’s always a chance to miss something. Linting helps catch these issues early during the development phase.

Go provides several built-in tools like gofmt for formatting and govet for reporting suspicious constructs. Additionally, the community maintains a vast number of specialized linters:
- fieldalignment: Detects structs that could use less memory if fields were sorted differently.
- unused: Checks for unused constants, variables, functions, and types.
- gofumpt: Enforces a stricter format than
gofmt. - goconst: Finds repeated strings that could be replaced by constants.
- gocyclo: Computes the cyclomatic complexity of functions.
- errcheck: Detects unchecked errors in Go programs.
Managing and running these tools individually can be slow and cumbersome.
🤓 Entering Golangci-lint
golangci-lint is a Go linters aggregator. It runs linters in parallel, reuses the Go build cache, and caches analysis results for much faster performance on subsequent runs.
It is fast, utilizes YAML-based configuration, has minimal false positives, and provides beautiful, colorized output. Most importantly, it includes many linters out of the box, so you don’t need to install them separately.
Installation
# Install to $(go env GOPATH)/bin/golangci-lint
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.45.2
# Verify installation
golangci-lint --version
Running the Linter
Let’s introduce a redundant import to see if golangci-lint catches it:
package main
import "fmt"
import "os" // Unused import
import "unsafe"
type TerraformResource struct {
Cloud string
Name string
HaveDSL bool
PluginVersion string
IsVersionControlled bool
TerraformVersion string
ModuleVersionMajor int32
}
func main() {
var d TerraformResource
d.Cloud = "aws"
fmt.Printf("Total Memory Usage StructType:d %T => [%d]\n", d, unsafe.Sizeof(d))
}
Now, run the linter:
>> golangci-lint run ./demo.go
demo.go:4:8: "os" imported but not used (typecheck)
import "os"
^
It automatically detected the unused import. golangci-lint supports over 100 linters. You can see the full list using:
>> golangci-lint help linters
Memory Alignment with fieldalignment
One particularly useful tool is fieldalignment. It identifies if your structs are optimized for memory usage. I wrote a detailed blog about memory alignment here.

>> fieldalignment demo.go
demo:6:24: struct of size 88 could be 72
Using these tools regularly ensures your code is not only correct but also efficient and maintainable.