有没有一种方法可以告诉编译器忽略未使用的导入?

huangapple go评论87阅读模式
英文:

Is there a way to flag compiler to ignore unused imports?

问题

如果编译器能够识别到一个导入语句未被使用,那么是否可以设置编译器在没有该导入语句的情况下继续编译?

即使这是不可能的,这样一个选项的优缺点是什么?

英文:

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 ?

答案1

得分: 12

不。有关推理,请参阅以下常见问题解答:

常见问题解答:我能停止关于未使用变量/导入的投诉吗?

> 未使用变量的存在可能表示存在错误,而未使用的导入只会减慢编译速度,这种影响在程序积累代码和时间的过程中可能变得很大。出于这些原因,Go拒绝编译具有未使用变量或导入的程序,以短期便利性换取长期的构建速度和程序清晰度。

> 然而,在开发代码时,临时创建这些情况是很常见的,而在程序编译之前将它们编辑掉可能会很烦人。

> 有些人要求添加一个编译器选项来关闭这些检查,或者至少将其减少为警告。尽管如此,没有添加这样一个选项,因为编译器选项不应影响语言的语义,并且Go编译器只报告阻止编译的错误,而不是警告。

> 没有警告的原因有两个。首先,如果值得抱怨,就值得在代码中修复。(如果不值得修复,就不值得提及。)其次,让编译器生成警告会鼓励实现警告弱案例,这可能会使编译变得嘈杂,掩盖了应该修复的真正错误。

> 不过,解决这种情况很容易。在开发过程中,使用空白标识符让未使用的内容保留下来。

当你临时想要排除某些内容时,你可以使用空白标识符,例如:

import (
    "fmt"
    _ "time"  // 这将使编译器停止抱怨
)

> 如今,大多数Go程序员使用一个工具,goimports,它会自动重写Go源文件以具有正确的导入,从而在实践中消除了未使用导入的问题。这个程序可以很容易地与大多数编辑器连接,在写入Go源文件时自动运行。

英文:

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.

huangapple
  • 本文由 发表于 2015年8月4日 00:26:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/31792281.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定