This question already has an answer here:
-
How to disable Golang unused import error
7 answers
If the compiler can recognize that an import is unused, then is it possible to set it to continue with compilation without that import ?
Even if it's not possible, what are the pros/cons of such an option ?
No. For reasoning see the following FAQ:
FAQ: Can I stop these complaints about my unused variable/import?
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.
Still, when developing code, it's common to create these situations temporarily and it can be annoying to have to edit them out before the program will compile.
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.
It's easy to address the situation, though. Use the blank identifier to let unused things persist while you're developing.
What you may do is use the blank identifier when temporarily want to exclude something, e.g.
import (
"fmt"
_ "time" // This will make the compiler stop complaining
)
Nowadays, most Go programmers use a tool, goimports, which automatically rewrites a Go source file to have the correct imports, eliminating the unused imports issue in practice. This program is easily connected to most editors to run automatically when a Go source file is written.