constantcheck is a golang analyzer for checking the possibility of replacing the literals used in the code with constants from already imported packages.
./constantcheck ./...
Let's say we have a project structure like this:
Structure | main.go | helps.go |
├── helps
│ ├── helps.go
├──main.go |
package main
import (
"fmt"
"example/helps"
)
func main() {
if err := helps.Ping(); err != nil{
fmt.Println("myImportantError")
}
} |
package helps
import (
"fmt"
)
const CustomError = "myImportantError"
func Ping() error{
fmt.Println("Pong")
return nil
} |
After checking the linter, we should get an error that we can use a constant from
helps
package with name CustomError
I couldn't show it in tests because there is some problem with imports in testing. But the linter parses ALL imported packages of the file, not just standard ones