My team has all our Golang code in a monorepo.
cmd
We've had it for a while and are doing some cleanup. Are there any tools or techniques that can find functions not used by the binaries under cmd
?
I know go vet
can find private functions that are unused in a package. However I suspect we also have exported library functions that aren't used either.
UPD 2020: The unused
tool has been incorporated into staticcheck
.
Unfortunately, v0.0.1-2020.1.4
will probably be the last to support this
feature. Dominik explains that it is because the check consumes a lot of
resources and is hard to get right.
To get that version:
env GO111MODULE=on go get honnef.co/go/tools/cmd/[email protected]
To use it:
$ staticcheck --unused.whole-program=true -- ./...
./internal/pkg/a.go:5:6: type A is unused (U1001)
Original answer below.
Dominik Honnef's unused
tool might be what you're looking for:
Optionally via the -exported flag, unused can analyse all arguments as a single program and report unused exported identifiers. This can be useful for checking "internal" packages, or large software projects that do not export an API to the public, but use exported methods between components.