initcheck

module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2024 License: Apache-2.0

README

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

  1. Download the package
go install github.com/patrickchense/initcheck/cmd@latest
  1. Run the checker

Run from source
  1. Build the project
go build -o ./bin/initchecker ./cmd
  1. Run against a file or package
./bin/initchecker ./test
Run with Golangci-lint

Directories

Path Synopsis
pkg

Jump to

Keyboard shortcuts

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