| Programming is the practice of telling the computer your intentions as code. Why are you upset that Go forces you to be explicit about your intentions, when it's ambiguous? E.g. take C++ single-argument constructors. It's the reason we have guidelines like this:
https://rules.sonarsource.com/cpp/RSPEC-1709 Go is opinionated on formatting and being explicit about unused imports. It's forcing you to do the right thing. C++ and Python let cruft accumulate, in headers and imports, with extra tooling external to the compiler (iwyu) to detect when you made a mistake. There are valid criticism of Go, and I have many, but this ain't one. > Why does it need to include any other unused module? Because without at the very least parse the entire dependency tree of the module (which could be huge) it cannot know if it's unused. Hell, it probably NEVER is unused, because many modules will have "var ErrFoo = errors.New(...)". If it calls C code in these global variables or "func init()" then there isn't even in theory a way that the compiler can deduce that it's unused. This is a solution to a real problem, where other languages take forever to build, and create bloated binaries. > Why does it need to include any other unused module? If you import it then it's not unused, and it's extremely common that that's a mistake. That's why. Have you ever coded C++, and done a refactor? How many times do you just leave "#include <sys/types.h>" because you don't know exactly why you imported it? Have you coded Python? Do you know that the previous author is not importing a given package for its side-effects? |
Mmm... No. It's just post-hoc justification of the design.
> > Why does it need to include any other unused module?
> Because without at the very least parse the entire dependency tree of the module (which could be huge) it cannot know if it's unused.
Once again: no. Because it already knows it's unused and stops the compilation. So it already knows it's unused.
> If you import it then it's not unused, and it's extremely common that that's a mistake. That's why.
Once again:
- the compiler has already parsed everything
- the compiler already knows that the import is unused
- the compiler stops the compilation at this point ... because otherwise it would have to include anyway even though at this point it already knows that the import is unused
> How many times do you just leave "#include <sys/types.h>" because you don't know exactly why you imported it?
There are other ways of dealing with this than halting the compilation entirely.