The use case for warnings is the exploratory development or debugging phases, where you really don't care about leaving unused things lying around for the time being (for example, temporarily commenting something out), and would rather that the compiler just got out of your way until you've got something ready to compile normally and commit.
Usage
go build -gcflags=-warnunused somefile.go
go test -gcflags=-warnunused
When you're done with your exploratory/debugging phase, simply build or test without the flag:
go build somefile.go
go test
Compiling without the flags will fail on unused things as normal.
Other comments are missing out the other key point: go packages are sometimes imported for side effects, and this pattern is common enough in the community (although albeit slightly discouraged now). So unused imports can still impact your code due to the init() functions
Unused imports that are imported solely for side effects should be imported this way to avoid that problem:
import (
_ "github.com/some/package"
)
Given that this is the only way to have unused imports with mainline Go — in other words, it's impossible to accidentally remove one once it's been declared this way — I'm not sure I see what the argument is.
"I really need to get this deployed to production. I'll remove the warning debugging flag and build it... AH MAN look at all these unused import error!! Screw it! I don't have time to fix them all. I'll just add it bad and fix it later"
That is a valid position, but imo at odds with the implicit position of Go's creators. Arguably, the Mommy regime of Go's compiler is very much reflective of Go's creators' appraisal of the software maturity of its intended users.
Regarding the point in general, the notion of "broken windows" is applicable.
It's the problem of "he knows what he's doing" vs "I know what I'm doing".
In the old unix days, tools were built under the assumption that "he knows what he's doing", meaning that if you typed "rm -r /" you probably had a good reason to do so. We've since learned that just blindly trusting potentially fat fingers is probably not the best approach, and so the more dangerous of those tools have been modified to require you to add additional flags to do the most dangerous things.
It was the same in C, where the compiler blindly did exactly as told until we realized that software developers have fat fingers too. Unfortunately, they got it backwards, issuing warnings that by default don't halt compilation ("he knows what he's doing"), which led users and managers to believe that warnings aren't serious enough to deal with.
What they SHOULD have done is made those warnings halt compilation by default, and only allow compilation to continue if the user had invoked an additional opt-in ("I know what I'm doing") flag.
There's also the issue of inconsistent warnings across compilers and the subtleties of UB in C that contributed to the warnings problem, but we don't have that in Go.
People often say this sort of thing about the constraints imposed by various systems. It's a bit of a rhetorical party trick, though, designed to avoid engaging with the design rationale of the constraints by appealing to the operator's vanity.
> Go's creators' appraisal of the software maturity of its intended users.
More like immaturity:
“The key point here is our programmers are Googlers, they’re not researchers. They’re typically, fairly young, fresh out of school, probably learned Java, maybe learned C or C++, probably learned Python. They’re not capable of understanding a brilliant language but we want to use them to build good software. So, the language that we give them has to be easy for them to understand and easy to adopt.”
"I really need to get this deployed to production. I'll
build it... AH MAN look at all these compilation errors and failing tests!! Screw it! I don't have time to fix them all. I'll just remove them and fix it later."
Use Case
The use case for warnings is the exploratory development or debugging phases, where you really don't care about leaving unused things lying around for the time being (for example, temporarily commenting something out), and would rather that the compiler just got out of your way until you've got something ready to compile normally and commit.
Usage
When you're done with your exploratory/debugging phase, simply build or test without the flag: Compiling without the flags will fail on unused things as normal.