Go语言的警告和错误

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

Go language warnings and errors

问题

GO语言似乎没有警告功能。我观察到了几个例子:

  1. "声明但未使用"(如果变量被声明但未在任何地方使用,会报错并且无法编译程序)
  2. "导入但未使用"(类似地,如果导入了包但未在任何地方使用,会报错并且无法编译程序)
    有人可以帮忙吗?如果有任何指导意见。
英文:

It seems that GO language does not have warnings in it. I've observed
few instances.

  1. "declared and not used"(if variable is declared and not used
    anywhere it gives an error and does not compile the program)
  2. "imported and not used"(similarly if package is imported and not
    used anywhere it gives an error and does not compile the program)
    Can somebody help. If they have any pointers.

答案1

得分: 22

Go正在努力防止这种情况发生:

男孩正在吸烟,并将烟圈吹向空中。女孩对烟雾感到恼火,对她的爱人说:“你看不见香烟包上写的警告吗?吸烟有害健康!”

男孩回答:“亲爱的,我是一个程序员。我们不担心警告,我们只担心错误。”

基本上,Go不会让你逃脱未使用的变量、未使用的导入和其他在其他语言中通常是警告的东西。它有助于养成良好的习惯。

英文:

Go is trying to prevent this situation:

> The boy is smoking and leaving smoke rings into the air. The girl gets
> irritated with the smoke and says to her lover: "Can't you see the
> warning written on the cigarettes packet, smoking is injurious to
> health!"
>
> The boy replies back: "Darling, I am a programmer. We don't worry
> about warnings, we only worry about errors."

Basically, Go just wont let you get away with unused variables and unused imports and other stuff that is normally a warning on other languages. It helps put you in a good habit.

答案2

得分: 14

Go编程语言常见问题解答

我能停止这些关于未使用变量/导入的投诉吗?

未使用变量的存在可能表示一个bug,而未使用的导入只会减慢编译速度。如果在代码树中累积了足够多的未使用导入,编译速度会变得非常慢。出于这些原因,Go语言不允许这两种情况发生。

在开发代码时,临时创建这些情况是很常见的,但在程序编译之前必须将它们删除可能会很烦人。

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

没有警告的原因有两个。首先,如果值得抱怨,就值得在代码中修复它。(如果不值得修复,就不值得提及。)其次,编译器生成警告会鼓励实现警告一些可能导致编译变得嘈杂的弱情况,从而掩盖了应该修复的真正错误。

不过,解决这个问题很容易。在开发过程中,可以使用空白标识符让未使用的内容保留下来。

import "unused"

// 这个声明通过引用包中的一个项来标记导入已被使用。
var _ = unused.Item  // TODO: 在提交之前删除!

func main() {
    debugData := debug.Profile()
    _ = debugData // 仅在调试期间使用。
    ....
}
英文:

> The Go Programming Language
> 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. Accumulate enough unused imports
> in your code tree and things can get very slow. For these reasons, Go
> allows neither.
>
> 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.
>
> import "unused"
>
> // This declaration marks the import as used by referencing an
> // item from the package.
> var _ = unused.Item // TODO: Delete before committing!
>
> func main() {
> debugData := debug.Profile()
> _ = debugData // Used only during debugging.
> ....
> }

答案3

得分: 1

一个解决未使用导入的方法是使用goimports,它是gofmt的一个分支。它会自动添加缺失的导入并删除未使用的导入(除了格式化你的代码)。

http://godoc.org/code.google.com/p/go.tools/cmd/goimports

我已经配置了我的编辑器,在我保存代码时自动运行goimports。现在我无法想象编写go代码时没有它。

英文:

One solution for unused imports is to use goimports, which is a fork of gofmt. It automatically adds missing imports and removes unused ones (in addition to formatting your code).

http://godoc.org/code.google.com/p/go.tools/cmd/goimports

I've configured my editor to automatically run goimports whenever I save my code. I can't imagine writing go code without it now.

答案4

得分: 0

根据我刚刚阅读的(维基百科),“Go的语法包括了一些从C语言中改变过来的东西,旨在保持代码简洁易读。”

“简洁”这个词对编译器来说非常重要。我发现编译器强制执行的语法不再是“\n”或空格不敏感的。而且没有“警告”类型的错误。

Go语言有一些好的地方,也有一些不太好的地方。不接受警告的态度有点极端,特别是在开发或测试新包时。似乎部分开发是不可接受的。警告是不可接受的。要么是生产版本,要么就是放弃。这是一种非常二元的观点。我想知道如果自然界受到这样的限制,进化是否会导致“生命”。

我只能希望事情会改变。有时候死亡似乎是非常有益的。我尝试过Go语言,但我感到失望。在我这个年纪,我不认为我会再回来了。

英文:

From what I just read, (wikipedia)
"Go's syntax includes changes from C aimed at keeping code concise and readable."

The word "concise" is very important to the compiler. I have found out
that the syntax enforced by the compiler is no longer "\n" or whitespace
agnostic. And there are no "warning" type errors.

There are good things about Go. There are some not so good things. The
attitude of no warnings is a bit extreme, especially when developing or testing
a new package. It seems that partial development is not acceptable. Warnings are not acceptable. It is either the production version or the highway. This is a very dualistic point of view. I wonder if evolution would have resulted in "life", if that had been the constraints on nature.

I can only hope that things will change. Death seems to be very beneficial at times.
I have tried Go, and I am disappointed. At my age I don't think I will return.

huangapple
  • 本文由 发表于 2011年8月15日 04:46:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/7059616.html
匿名

发表评论

匿名网友

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

确定