Source code analyzer that helps you to make your Go programs more consistent.
Source code analyzer that helps you to make your Go programs more consistent.
Source code analyzer that helps you to make your Go programs more consistent.
This install the go-consistent binary:
go install github.com/quasilyte/go-consistentIf go install location is under your system $PATH, go-consistent command should be available after that.
This should print the help message:
go-consistent --helpYou can pass package names and separate Go filenames to the go-consistent tool:
go-consistent foo.go bar.go mypkgYou can also use std, ./... and other conventional targets that are normally
understood by Go tools.
To check the entire project, run go-consistent like this:
go-consistent -v ./...To understand what go-consistent does, take a look at these 3 lines of code:
lit1 := map[K]V{}
lit2 := map[K]V{}
m := make(map[K]V)lit1, lit2 and m are initialized to an empty, non-nil map.
The problem is that you have at least 2 ways to do it:
lit1 and lit2 use the first option, the map literalm uses the second option, the make functionNeither of these are the "best", but on the package or project level, you might want to prefer only one of them, for consistency reasons.
go-consistent tool detects that map literal used more frequently (2 vs 1) in the example above,
so it suggest you to replace m initialization expression to use map literal instead of make function.
There are many similar cases where you have 2 or more options of expressing the same thing in Go,
go-consistent tries to handle as much patterns as possible.
Checkers that require types info:
Checkers that do not require types info:
// A: no parenthesis
import "fmt"
// B: with parenthesis
import (
"fmt"
)// A: new call
new(T)
new([]T)
// B: address of literal
&T{}
&[]T{}// A: make call
make([]T, 0)
// B: slice literal
[]T{}// A: make call
make(map[K]V)
// B: map literal
map[K]V{}// A: lower case a-f digits
0xff
// B: upper case A-F digits
0xFF// A: left-aligned
x > low && x 0
// C: compare as "at least 1 element"
len(xs) >= 1// A: default case is the first one
switch {
default:
return "?"
case x > 10:
return "more than 10"
}
// B: default case is the last one
switch {
case x > 10:
return "more than 10"
default:
return "?"
}No open issues yet, or sync has not completed.