A Go static analysis tool that finds repeated string and number literals that could be replaced by constants.
goconst is a Go static analysis tool that scans codebases for repeated string and number literals that could be replaced by constants. It helps developers identify "magic strings" and duplicated values that make code harder to maintain, suggesting where constants would improve readability and reduce errors.
Go developers working on medium to large codebases who want to improve code quality and maintainability, particularly teams concerned with eliminating technical debt and repetition.
goconst provides specialized, configurable detection of literal repetition that general linters might miss, with features like Unicode-aware length checking, constant expression evaluation, and flexible filtering options tailored for real-world Go projects.
Find in Go repeated strings that could be replaced by a constant
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Measures string length in runes, not bytes, ensuring accurate detection for multi-byte characters as per the README, which prevents miscounts in internationalized code.
Allows ignoring tests by default, excluding specific function calls with -ignore-calls, and using regex patterns, making it adaptable to real-world project needs without manual file editing.
With the -eval-const-expr flag, it can evaluate expressions like Prefix + 'suffix', enhancing detection in complex code where constants are built dynamically.
Supports JSON formatting via -output json, enabling easy integration into CI/CD pipelines and automated tooling for code quality reports.
Only detects identical, complete literals; repeated substrings within larger strings are ignored, which could miss duplication in templated or generated strings.
By default, skips existing constant declarations, requiring explicit flags like -match-constant for full analysis, which users might overlook, leading to incomplete scans.
Without careful configuration (e.g., using -ignore-calls), it can flag intentional repetitions like error messages or log strings, adding overhead to filter out false positives.