by dmitriid · 5y ago · 0 comments
I'm reading this and I can't shake the feeling that this is really bad compiler and/or language design.
Why would the compiler import (that is, parse and compile) a package that's unused in the code?
> A better solution is to use goimports tool. Goimports removes unreferenced imports for you. What’s even better it tries to automatically find and add missing ones.
Why can't the compiler do that? Why do you need a few dozen separate tools to complement the compiler?
Parse - yes, compile - no. Compiler still needs to parse your code to determine if your import is used or not.
Go team decided that compilation should take as little time as possible, so it is easier to make sure that the compiler does not have to worry about unused imports at all.
Not to mention that this keeps your code clean.
Dozen separate tools can be used effectively in your workflow (say CI\CD pipeline). Or just executed on every save. (or both)
Parsing is easy. And it's fast. On top of that the compiler already knows it's unused:
--- start quote ---
Go programs with unused imports do not compile. This is a deliberate feature of the language as importing packages slow down the compiler.
--- end quote ---
So, the compiler has already parsed the file, and determined that the import is unused. And refuses to compile further, because... it needs to import that package anyway? Why?
> Dozen separate tools can be used effectively in your workflow
Haha, no. If you need a dozen different tools, there's no question about effectiveness at all.
Because it may have side-effects, and the compiler doesn't know if it's the programmer's intention to import in order to get those side-effects, or if it's a mistake.
I'm sorry to hurt your feelings, but compiler definitely can know whether some external module is used in the file it's compiling now.
See my other comment: https://news.ycombinator.com/item?id=26553401
Without opening more files and parsing them, how does it know that none of the transitive dependency tree has a `func init(){}` ?
Go forces you to add an underscore if you mean "I'm importing this for its side-effects, and am not using it directly".