4 ms·
Just hook your editor up to run goimports on save.
by AlexCoventry 7y ago
Just hook your editor up to run goimports on save.
- jakear 7y agoDoesn't help when you remove chunks for testing purposes, but want to keep the imports around as you'll be adding them back immediately after and don't want to search for the right import again.
- DuskStar 7y agoMy experience with GoLand is that it manages to add back the right imports (notably with things like errors vs github.com/pkg/errors vs github.com/cockroachdb/errors, where they all share a package name and ~interface) but that might just be me getting lucky or not noticing changes.
- kstenerud 7y agoYou got lucky. And it still doesn't solve the problem of unused variables. At the end of the day, it's a hair shirt. And no matter how many coping mechanisms people come up with, it's still a hair shirt. Even the go authors had to put a limit on their madness. You'll notice that unused unexported functions don't cause compilation errors despite the fact that they, too, fall afoul of the original justification for this policy: https://golang.org/doc/faq#unused_variables_and_imports https://golang.org/doc/faq#unused_variables_and_imports "The presence of an unused variable may indicate a bug, while unused imports just slow down compilation, an effect that can become substantial as a program accumulates code and programmers over time. For these reasons, Go refuses to compile programs with unused variables or imports, trading short-term convenience for long-term build speed and program clarity." But disallowing unused functions would have been a bridge too far, so we're left with this half-measure and half-reasoning that doesn't even make sense.
- omegabravo 7y ago> But disallowing unused functions would have been a bridge too far, so we're left with this half-measure and half-reasoning that doesn't even make sense. how would you find an unused function?
- kstenerud 7y agoThe same way you detect unused variables: Look for accesses, and if nothing accesses the function, it's unused. Note that I'm talking about non-exported functions (i.e. someFunction rather than SomeFunction).
- sethammons 7y agowhile a pain, for unused variables, if it comes up, right under the declared variable, just add the line: _ = myVar. Bingo, now it is used. Gross, but works.
- myrloc 7y agoI think you missed the point of the comment - no matter if you're removing or adding imports, the tool (goimports) should do it for you automatically. It's easy to setup with any modern text editor (VSCode, emacs, etc)
- jakear 7y agoWhat of when there are duplicate symbols with the same name? And this doesn’t help with unused locals. Also, another commenter pointed out that unused local functions don’t get flagged. Go authors probably realized that making people delete and restore entire function bodies during debugging was too much, but they could still convince people to be okay with deleting and restoring import statements and local variable definitions.
- AlexCoventry 7y agoIt's much rarer that an unused function indicates an error.
- myrloc 7y agoExactly. Imports should never be an issue with the tooling available, i.e. goimports, and almost any text editor that can run the tool on save.