goconst is a static analysis utility for Go that finds repeated literals which may be better represented as constants.
Replacing duplicated values with named constants can reduce maintenance work and make intent clearer, particularly in larger codebases where the same literal may appear across multiple files or packages. The program analyses source syntax rather than simply searching text, which lets it apply Go-aware filtering and report useful source locations.
This is free and open source software.
Key Features
- Detects repeated string literals and can optionally inspect duplicated numeric literals.
- Lets users set the minimum number of occurrences before a value is reported.
- Supports a minimum string length measured in runes, so multi-byte characters are handled correctly.
- Can search for existing constants whose values match repeated literals.
- Can identify separate constants that share the same value.
- Optionally evaluates constant expressions when matching against existing constants.
- Can ignore test files during analysis.
- Offers regular-expression filtering for files and literal values that should be skipped.
- Can ignore literals passed to selected functions such as logging or error-formatting calls.
- Can omit literals used inside composite literals or as map keys.
- Provides minimum and maximum value filters when checking numbers.
- Supports text and JSON output, including grouped text output.
- Can return a distinct exit status when findings are detected, which is useful in automated checks.
- Supports analysis of multiple directories and Go package trees from the command line.
Website: github.com/jgautheron/goconst
Support:
Developer: jgautheron
License: MIT License
goconst is written in Go. Learn Go with our recommended free books and free tutorials.
Related Software
| Go Linters | |
|---|---|
| revive | Designed as a drop-in replacement for golint |
| golangci-lint | Fast Go linters runner |
| gosec | Security scanner for Go projects |
| Staticcheck | Advanced Go linter |
| go-critic | Opinionated Go source code linter |
| Vet | Examines Go source code and reports suspicious constructs |
| gofumpt | Stricter gofmt |
| gofmt | Formats Go programs |
| go-ruleguard | Analysis-based Go linter that runs dynamically loaded rules |
| forbidigo | Go linter for forbidding identifiers |
| godot | Keep source code comments consistent |
Read our verdict in the software roundup.
Explore our carefully curated directory of recommended free and open source software, covering every major software category.The directory forms part of our extensive collection of articles for Linux enthusiasts. It includes hundreds of detailed reviews, together with free and open source alternatives to proprietary software from companies such as Google, Microsoft, Apple, Adobe, IBM, Cisco, Oracle, and Autodesk. LinuxLinks also covers interesting projects worth exploring, Linux-compatible hardware, free programming books and tutorials, and much more. Know a useful free and open source Linux application that we haven’t covered? Tell us about it using our submission form. |


Please read our Comment Policy before commenting.