gpt4 book ai didi

compiler-errors - go 和 gwan 未使用的变量

转载 作者:IT王子 更新时间:2023-10-29 02:26:05 26 4
gpt4 key购买 nike

Go 编译器提示未使用的变量和导入。因此,如果检测到任何未使用的变量/脚本,则无法在 gwan 中运行 go 脚本。

在这种情况下,gwan 会在运行时提供 404 错误...或者根本无法启动

有办法避免这种行为吗?

最佳答案

不存在真正简单的方法。没有什么比编译器标志更能关闭此行为了。我想最好传递编译器可以首先吞下的代码。

编辑:常见问题解答中的 C/P: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.
....
}

关于compiler-errors - go 和 gwan 未使用的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14397109/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com