Today I Learned: collection of notes, tips and tricks and stuff I learn from day to day working with computers and technology as an open source contributor and product manager

View project on GitHub

Use gocritic

I come from a background of heavy use of Perl::Critic, so when I fell over gocritic and found it an interesting tool.

The installation documentation, does seem completely up to date, since it describes go get, which is deprecated in newer Go versions.

I got this invocation to work:

go install github.com/go-critic/go-critic/cmd/gocritic@latest

I install it in my $HOME directory in my GOPATH and I can use it in all my Go projects.


Here is a basic example, lifted from the documentation and changed.

package main

func main() {
    cond := 1

    if cond <= 1 {
        // Code A.
    } else if cond >= 2 {
        // Code B.
    } else {
        // Code C.

And when running gocritic with the check command

$ gocritic check main.go
./main.go:8:9: dupBranchBody: both branches in if statement has same body
./main.go:6:2: ifElseChain: rewrite if-else to switch statement

This is a very basic and caricatured example, but you get the picture.

Resources and References

  1. GitHub: go-critic
  2. go-critic.com
  3. Go documentation on “Add dependencies to current module and install them”