Skip to content

Go R1 Day 77

progress🔗

More Fun With Golangci-lint🔗

Been doing a ton the last month with golangci-lint tooling. I think this has been one of the most educational tools for learning Go I've come across. It has forced me to evaluate why issues are flagged, if they are opinionated preferences or best practices.

For example, wsl ensures that statements are not cuddled. This follows Dave Cheney's writing about having an empty line seperate phrases of thought.

It's a bit annoying to implement as a linter though, and can't be applied programaticaly so I'm not using that.

Linting - Shadowing Package Names🔗

Another one that I caught from Goland linting today, that golangci-lint didn't seem to catch, was the shadowing of a package name.

In this scenario I found code where:

package taco


func main() {
 taco := taco.Method()
}

While this is legal, it's a confusing practice, and thereafter prohibits the usage of the taco package as it's been overshadowed by the variable.

To me this is a clear violation of Go's preference for "no magic" and readability.

In this scenario, the fix is simple. Change the variable name used or alias the package (my preference).

package (
  pkgtaco "taco"
)


func main() {
 taco := pkgtaco.Method()
}

Linting - Handling Errors🔗

Also did some investigation on errcheck and flagging of handling file close and response body closing for http. This is one of those areas that linters flag and it's a "it depends" and not very consistent.

Basically the gist is ignore, except if file writing is occuring then it's probably needing an explicit handle.