Use goconst to find repeated, exact string literals in Go code that may be worth turning into constants. Install it with Go, then run it across your project:
go install github.com/jgautheron/goconst/cmd/goconst@latest
goconst ./...
A report is a prompt for review, not a command to extract every match: repeated values should become one constant only when they represent the same concept.
As an Amazon Associate I earn from qualifying purchases.
What goconst finds
goconst analyzes Go source for string literals that occur multiple times. It can also check numeric literals when configured to do so. The tool is intended to surface values that could be replaced by constants, which can make a value easier to maintain when it must change in several places.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
It compares complete, unquoted literal values. For example, two occurrences of "connection failed" match; "connection failed" and "connection failed: retrying" do not match just because they share words. It is not a substring search.
Install and run it
- Install the command with Go:
go install github.com/jgautheron/goconst/cmd/goconst@latest - From the project directory, scan packages and subdirectories with
goconst ./.... - Review the reported locations and decide whether each repeated value expresses one shared concept before changing the code.
The command accepts directories to analyze. Use goconst -h with the version you installed to check its current flags and defaults; options can change between releases.
Choose what counts as a finding
The default filters help limit reports to likely candidates, and the CLI offers controls for adjusting the scan:
-min-occurrencessets the number of appearances required; the documented default is 2.-min-lengthsets the minimum string length; the documented default is 3. Length is counted in Unicode runes, not bytes.-ignoreand-ignore-stringslet you exclude matching paths or literal values.-ignore-testsis documented as true by default.-match-constantchecks literals against existing constants. Constant declarations are otherwise skipped by default.-find-duplicateslooks for constants that have identical values.-eval-const-exprcontrols evaluation of constant expressions, while-ignore-calls,-ignore-composite-literals, and-ignore-map-keyscontrol exclusions for particular contexts.-numbersenables numeric-literal checks;-minand-maxset a numeric range.-outputselects output format, including JSON in the documentation examples;-groupedgroups findings, and-set-exit-statusmakes findings affect the command’s exit status.
Consult the installed command’s help for accepted values and behavior before relying on a flag in a script or CI check.
Decide whether a match deserves a constant
First ask whether the occurrences mean the same thing. A repeated error message or shared protocol value may have one owner and benefit from a single definition. Two identical short strings used independently in different contexts may not: extracting them can create an artificial dependency or make the code harder to understand.
Also consider scope and naming. A constant should be visible where it is needed, named for the concept it represents, and easy to update without coupling unrelated code. goconst identifies textual repetition; it does not determine intent, choose a good name, or decide where the constant belongs.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Run it standalone or through golangci-lint
You can run the standalone command shown above, or configure goconst as part of golangci-lint. The project documents both routes. Choose based on where your team already configures lint checks, how developers and CI run them, and whether you need particular thresholds or exclusions. The golangci-lint reference includes settings for minimum length and occurrences, matching constants, duplicate numbers, numeric ranges, ignored calls, and excluded contexts; confirm the available settings and defaults for the release you use.
Rank #4
The documentation establishes these usage options, but does not provide a controlled comparison of their speed or maintenance impact. Choose a route based on your workflow rather than assuming one is faster or yields fewer false positives.
The Tool Desk
Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




