initcheck
Go linter to check initialization of various Go types, such as slice, map, struct etc,.
Code Style
initchecker aims to detect "smelly" Go code in initialization. To follow the guidelines from Uber style guide, initchecker detects below code as "bad".
nil is a valid slice
// "bad" code
a := []int{}
// "good" code
var a []int
Local Variable Declaration
// "bad" code
var c = "foo"
// "good" code
c := "foo"
Omit Zero Value Fields in Structs
// "bad" code
Order struct {
id int
name string
num int
price double
}
o1 := new Order{
id: 111111,
name: "",
num: 0,
price: 0.0,
}
// "good" code
o1 := new Order{
id: 111111,
}
Use var for Zero Value Structs
// "bad" code
o2 := Order{}
// "good" code
var o2 Order
Initializing Maps with make
// "bad" code
m1 = map[T1]T2{}
// "good" code
m1 = make(map[T1]T2)
Run initchecker
- Download the package
go install github.com/patrickchense/initcheck/cmd@latest
- Run the checker
Run from source
- Build the project
go build -o ./bin/initchecker ./cmd
- Run against a file or package
./bin/initchecker ./test
Run with Golangci-lint