constantcheck

module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 5, 2023 License: MIT

README

constantcheck


constantcheck is a golang analyzer for checking the possibility of replacing the literals used in the code with constants from already imported packages.

Installation
go get -u github.com/AntonBraer/constantcheck@latest
Usage
./constantcheck ./...
Examples

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

Directories

Path Synopsis
cmd
pkg

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL