如何禁用Golang的未使用导入错误

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

How to disable Golang unused import error

问题

默认情况下,Go将未使用的导入视为错误,强制你删除该导入。我想知道是否有一些方法可以改变这种行为,例如将其降级为警告。

我发现这个问题非常令人恼火,让我无法享受在Go中编码的乐趣。

例如,我正在测试一些代码,禁用了一段/函数。某些来自库的函数不再使用(例如fmt、errors等),但是在稍微测试一下后,我需要重新启用该函数。现在,除非我删除这些导入,否则程序将无法编译,而几分钟后我又需要重新导入该库。

在开发GAE程序时,我一遍又一遍地进行这个过程。

英文:

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.

答案1

得分: 52

在包名前面添加下划线(_)将忽略未使用的导入错误。

以下是一个示例,展示了如何使用它:

import (
	"log"
	"database/sql"

	_ "github.com/go-sql-driver/mysql"
)

> 如果只为了其副作用(初始化)而导入一个包,请使用空白标识符作为显式包名。

更多信息请参考 https://golang.org/ref/spec#Import_declarations

英文:

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

答案2

得分: 32

var _ = fmt.Printf 这个技巧在这里很有帮助。

英文:

The var _ = fmt.Printf trick is helpful here.

答案3

得分: 29

我有同样的问题。我理解他们实施禁止未使用的导入和变量的语言的原因,但我个人在编写代码时觉得这个特性很烦人。为了解决这个问题,我改变了我的编译器,允许在我的代码中使用可选标志来允许未使用的变量和导入。

如果你有兴趣,你可以在https://github.com/dtnewman/modified_golang_compiler 上看到这个。

现在,我只需要运行一个命令,比如"go run -gcflags '-unused_pkgs' test.go",就不会抛出这些"未使用的导入"错误。如果我省略这些标志,它就会返回到默认状态,不允许未使用的导入。

这只需要做一些简单的改动。Go 的纯粹主义者可能不会对这些改动感到满意,因为不允许未使用的变量/导入是有充分理由的,但我个人同意你的观点,这个问题使得在 Go 中编码变得不那么愉快,所以我对我的编译器进行了这些改动。

英文:

I have the same problem. I understand the reasoning for why they implemented the language to disallow unused imports and variables, but I personally find this feature annoying when writing my code. To get around this, I changed around my compiler to allow optional flags for allowing unused variables and imports in my code.

If you are interested, you can see this at https://github.com/dtnewman/modified_golang_compiler.

Now, I can simply run code with a command such as <b>go run -gcflags '-unused_pkgs' test.go</b> and it will not throw these "unused import" errors. If I leave out these flags, then it returns to the default of not allowing unused imports.

Doing this only required a few simple changes. Go purists will probably not be happy with these changes since there is good reason to not allow unused variables/imports, but I personally agree with you that this issue makes it much less enjoyable to code in Go which is why I made these changes to my compiler.

答案4

得分: 20

使用goimports。它基本上是gofmt的一个分支,由Brad Fitzpatrick编写,现在已包含在go工具包中。你可以配置你的编辑器,在每次保存文件时运行它。你将不再需要担心这个问题。

英文:

Use goimports. It's basically a fork of gofmt, written by Brad Fitzpatrick and now included in the go tools packages. You can configure your editor to run it whenever you save a file. You'll never have to worry about this problem again.

答案5

得分: 7

使用 if false { ... } 来注释掉一些代码。花括号内的代码必须在语法上是正确的,但除此之外可以是无意义的代码。

英文:

Use if false { ... } to comment out some code. The code inside the braces must be syntactically correct, but can be nonsense code otherwise.

答案6

得分: 4

如果你在开发和测试过程中使用fmt包来进行一般的控制台打印,那么你可能会在log包中找到更好的解决方案。

英文:

If you are using the fmt package for general printing to console while you develop and test then you may find a better solution in the log package.

答案7

得分: 2

很多人已经用有效的理由发表了评论,我也理解原作者的意图。然而,Rob Pike在不同的论坛上提到,Go语言是对其他一些主流编程语言中缺乏或难以实现的过程进行简化的结果。Go语言的语义以及为了加快编译速度而采用了很多看起来效率低下的方法。

简而言之,在Go语言中,未使用的导入被视为错误,因为它会使程序变得臃肿并减慢编译速度。使用导入进行副作用(_)是一种解决方法,但是当有效的导入与具有副作用的导入混合在一起,并且还有纯粹用于调试/测试目的的副作用导入时,我有时会感到困惑,特别是当代码库很大时,有可能会忘记删除不必要的导入,这可能会在以后困惑其他工程师/评审人员。我过去常常注释掉未使用的导入,但是像VS Code和Goland这样的流行IDE可以很容易地使用goimports,它可以很好地插入和删除导入。请参考链接了解更多信息:https://golang.org/doc/effective_go.html#blank_import

英文:

A lot of people have already commented with valid justification and I also acknowledge the original author's intention. However, Rob Pike mentioned in different forums that Go is the outcome of simplification of the processes that a few other mainstream programming languages either lack or not easy to achieve. It's Go's language semantics as well as to make the compilation faster, there are a lot of things that are adopted which initially seems inefficient.

To make it short, unused imports are considered as errors in Go as it blots the program and slows down the compilation. Using import for side effect (_) is a workaround, however, I find this confusing at times when there is a mix of valid imports with side effects along with side effects imported purely for the purpose of debugging/testing especially when the code base is large and there is a chance to forget and not delete unintentionally which may confuse other engineers/reviewers later. I used to comment out the unused ones, however, popular IDEs such as VS code and Goland can use goimports easily which does the insertion and deletion of the imports pretty well. Please refer to the link for more info, https://golang.org/doc/effective_go.html#blank_import

答案8

得分: 1

将以下内容放在你的文档顶部,忽略未使用的导入:

import (
	"bufio"
	"fmt"
	"os"
	"path/filepath"
)

var _, _, _, _ = fmt.Println, bufio.NewReader, os.Open, filepath.IsAbs
英文:

put this on top of your document and forget about unused imports:

import (
	&quot;bufio&quot;
	&quot;fmt&quot;
	&quot;os&quot;
	&quot;path/filepath&quot;
)

var _, _, _, _ = fmt.Println, bufio.NewReader, os.Open, filepath.IsAbs

答案9

得分: 0

我刚刚制作了一个新的Golang编译器,默认情况下会忽略所有的"变量未使用警告"、"导入未使用警告"和"函数未使用警告"。

https://github.com/yingshaoxo/go/releases/tag/v1.21

它适用于"go run"和"go build"命令。

英文:

I just made a new Golang compiler, which will simply ignore all variable unused warning and import unused warning and function unused warning by default.

https://github.com/yingshaoxo/go/releases/tag/v1.21

It works for both go run and go build

huangapple
  • 本文由 发表于 2013年10月24日 16:05:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/19560334.html
匿名

发表评论

匿名网友

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

确定