有没有办法标记编译器忽略未使用的导入? [英] Is there a way to flag compiler to ignore unused imports?

查看:70
本文介绍了有没有办法标记编译器忽略未使用的导入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果编译器可以识别出未使用导入,那么是否可以将其设置为不进行导入而继续编译?

If the compiler can recognize that an import is unused, then is it possible to set it to continue with compilation without that import ?

即使不可能,

推荐答案

否。要进行推理,请参阅以下常见问题解答:

No. For reasoning see the following FAQ:

常见问题解答:可以我停止了有关未使用的变量/导入的这些投诉吗?


存在未使用的变量可能表示存在错误,而未使用导入只会减慢编译速度,随着程序随着时间的推移积累代码和程序员,这种影响会变得很明显。由于这些原因,Go拒绝使用未使用的变量或导入来编译程序,为了长期构建速度和程序清晰性而以短期便利为代价。

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.

有人要求使用编译器选项来关闭这些情况检查或至少将其减少为警告。不过,由于编译器选项不应影响语言的语义,并且因为Go编译器不报告警告,因此仅添加阻止编译的错误。

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
)




如今,大多数Go程序员使用 goimports 工具,该工具会自动将Go源文件重写为具有正确的进口,消除了实践中未使用的进口问题。编写Go源文件时,该程序可以轻松连接到大多数编辑器,以自动运行。

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.

这篇关于有没有办法标记编译器忽略未使用的导入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆