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 |
| goconst | Find repeated Go literals that could be replaced with constants |
| go-ruleguard | Analysis-based Go linter that runs dynamically loaded rules |
| sloglint | Enforce consistent structured logging conventions in Go |
| forbidigo | Go linter for forbidding identifiers |
| godot | Keep source code comments consistent |
| Godoc-Lint | Enforce consistent documentation comments in Go code |
| exhaustive | Check Go switch statements for missing enum cases |
Read our verdict in the software roundup.
LinuxLinks helps you discover the best free and open source software for Linux. Explore our extensive collection of recommended applications, including alternatives to popular proprietary software.We also publish software reviews, Linux tutorials, hardware reviews, free programming books and articles covering the wider open source community. Browse our Linux resources to discover more. Know a useful free and open source Linux application we haven't covered? Suggest a program for inclusion. Has your software been featured on LinuxLinks? Display one of our Featured on LinuxLinks badges on your website or GitHub README. |


Please read our Comment Policy before commenting.