如何禁用 Golang 未使用的导入错误 [英] How to disable Golang unused import error
问题描述
默认情况下,Go 将未使用的导入视为错误,强制您删除导入.我想知道是否存在改变这种行为的希望,例如将其降低为警告.
我觉得这个问题非常烦人,使我无法享受在 Go 中编码的乐趣.
例如,我正在测试一些代码,禁用一个段/函数.不再使用 lib 中的某些函数(例如 fmt、错误等),但我需要在进行一些测试后重新启用该函数.现在除非我删除这些导入,否则程序将无法编译,几分钟后我需要重新导入 lib.
在开发 GAE 程序时,我一次又一次地执行此过程.
在包名前添加下划线 (_
) 将忽略未使用的导入错误.
这是一个如何使用它的示例:
import (日志"数据库/sql"_ "github.com/go-sql-driver/mysql")
<块引用>
要仅为其副作用(初始化)导入包,请使用空白标识符作为显式包名称.
在 https://golang.org/ref/spec#Import_declarations 处查看更多信息>
By default, Go treats unused import as error, forcing you to delete the import. I want to know if there exists some hope to change to this behavior, e.g. reducing it to warning.
I find this problem extremely annoying, preventing me from enjoying coding in Go.
For example, I was testing some code, disabling a segment/function. Some functions from a lib is no longer used (e.g. fmt, errors, whatever), but I will need to re-enable the function after a little testing. Now the program won't compile unless I remove those imports, and a few minutes later I need to re-import the lib.
I was doing this process again and again when developing a GAE program.
Adding an underscore (_
) before a package name will ignore the unused import error.
Here is an example of how you could use it:
import (
"log"
"database/sql"
_ "github.com/go-sql-driver/mysql"
)
To import a package solely for its side-effects (initialization), use the blank identifier as explicit package name.
View more at https://golang.org/ref/spec#Import_declarations
这篇关于如何禁用 Golang 未使用的导入错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!