According to the FAQ:
Some have asked for a compiler option to turn those checks off or at least reduce them to warnings. Such an option has not been added, though, because compiler options should not affect the semantics of the language and because the Go compiler does not report warnings, only errors that prevent compilation.
There are two reasons for having no warnings. First, if it's worth complaining about, it's worth fixing in the code. (And if it's not worth fixing, it's not worth mentioning.) Second, having the compiler generate warnings encourages the implementation to warn about weak cases that can make compilation noisy, masking real errors that should be fixed.
I don't necessarily agree with this for various reasons not worth going into. It is what it is, and it's not likely to change in the near future.
For packages, there's the goimports
tool which automatically adds missing packages and removes unused ones. For example:
# Install it
$ go get golang.org/x/tools/cmd/goimports
# -w to write the source file instead of stdout
$ goimports -w my_file.go
You should be able to run this from any half-way decent editor − for example for Vim:
:!goimports -w %
The goimports
page lists some commands for other editors, and you typically set it to be run automatically when you save the buffer to disk.
Note that goimports
will also run gofmt
.
As was already mentioned, for variables the easiest way is to (temporarily) assign them to _
:
// No errors
tasty := "ice cream"
horrible := "marmite"
// Commented out for debugging
//eat(tasty, horrible)
_, _ = tasty, horrible
_
is still a very cumbersome work around which demonstrably breaks developer flow. Every single time you comment something out you then need to go and stub all thedeclared but not used
errors. Any team could implement standards and style checks as a commit hook or part of a CI/Gate job. The lack of agency given to developers here is patronizing and furthermore forces us to write worse code in order to be able to pull things apart.goimports
solves the issue for imports, a compiler flag should be acceptable for variables. – Simon Merrick