Categorygithub.com/nathants/go-hasdefer
repositorypackage
0.0.0-20240217094046-52a470f29dcd
Repository: https://github.com/nathants/go-hasdefer.git
Documentation: pkg.go.dev

# README

go-hasdefer

what

a go linter to check that all goroutines have a defer statement.

why

it's too easy to miss panics when they happen inside goroutines, since they exit the defer scope of the caller.

install

go install github.com/nathants/go-hasdefer@latest

usage


>> go-hasdefer $(find .test/good/ -name '*.go')

>> go-hasdefer $(find .test/bad/ -name '*.go')
missing defer anon func oneliner:        .test/bad/bad_onliner.go:4      go func() {}()
missing defer anon func multiliner:      .test/bad/bad_multiliner.go:4   go func() {
missing defer top level func multiliner: .test/bad/bad_import.go:12 func Foobar() {
missing defer top level func oneliner:   .test/bad/bad_import.go:6 func (d *Data) Foobar2() {}
missing defer named func multiliner:     .test/bad/bad_imported.go:8     Foobar3 := func() {
missing defer top level func multiliner: .test/bad/bad_import.go:8 func (d *Data) Foobar4() {
missing defer named func oneliner:       .test/bad/bad_imported.go:12    Foobar4 := func() {}

notes

either of these is valid:

go func() {
    defer func() {}()
}()
go func() {
   // defer func() {}()
}()

the purpose of this linter is to force you to consider what happens if a goroutine panics, not to force you to include empty defers.

this is similar to always considering what to do with an err, even if you decide to assign it to _.