使用Go和Gwan时未使用的变量

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

Unused variable with go and gwan

问题

Go编译器会抱怨未使用的变量和导入。因此,如果检测到任何未使用的变量/脚本,Go脚本无法在Gwan中运行。

在这种情况下,Gwan会提供一个404错误,如果运行...或者根本无法启动。

有没有办法避免这种行为?

英文:

Go compiler complain about unused variable and imports. So go scripts can't be run within gwan if any unused var/script is detected.

gwan provide a 404 error in this case if running ... or simply can't be start

There is a way to avoid this behavior?

答案1

得分: 8

没有真正简单的方法。没有像编译器标志那样可以关闭此行为的东西。我猜最好的办法是传递编译器可以接受的代码。

编辑:从常见问题中复制/粘贴:我可以停止关于未使用变量/导入的投诉吗?

> 未使用的变量的存在可能表示一个错误,而未使用的导入只会减慢编译速度。在代码树中积累足够多的未使用导入,编译速度会变得非常慢。出于这些原因,Go 两者都不允许。
>
> 在开发代码时,临时创建这些情况是很常见的,而在程序编译之前将它们删除可能很烦人。
>
> 有些人要求添加一个编译器选项来关闭这些检查,或者至少将它们减少为警告。尽管如此,还没有添加这样的选项,因为编译器选项不应该影响语言的语义,并且 Go 编译器只报告阻止编译的错误,而不是警告。
>
> 没有警告的原因有两个。首先,如果值得抱怨,就值得在代码中修复。(如果不值得修复,就不值得提及。)其次,编译器生成警告会鼓励实现警告弱情况,这可能会使编译变得嘈杂,掩盖了应该修复的真正错误。
>
> 不过,解决这个问题很容易。使用空白标识符,让未使用的内容在开发过程中保留。

import "unused"

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

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

No really easy way exists. There's nothing like a compiler flag to turn this behavior off. I guess it's just better to pass around code which the compiler can swallow in the first place.

EDIT: C/P from the 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.
        ....
}

答案2

得分: -1

这是一个重复的Go语言问题,请参见:

Go:“变量声明但未使用”编译错误

Go编译器将"未使用的变量"视为致命错误。这是其他所有语言都将其视为警告的事情,因此G-WAN停止让您修复脚本错误。

在这里,G-WAN无法帮助您:您必须按照预期使用Go。

英文:

That's a duplicate Go language question, see:

Go: “variable declared and not used” compilation error

The Go compiler treats "unused variable" as a fatal error. This is something that all other languages treat as a warning, hence G-WAN stopping to let you fix the script error(s).

There's nothing G-WAN can do to help you here: you must use Go as expected.

huangapple
  • 本文由 发表于 2013年1月18日 18:37:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/14397109.html
匿名

发表评论

匿名网友

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

确定