简体   繁体   English

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

[英]“variable declared and not used” compilation error

I am learning Google's new language Go .我正在学习谷歌的新语言Go I am just trying stuff out and I noticed that if you declare a variable and do not do anything with it the go compiler ( 8g in my case) fails to我只是在尝试一些东西,我注意到如果你声明一个变量并且不对它做任何事情,go 编译器(在我的例子中是8g )将无法
compile with this error: hello.go:9: error declared and not used .编译时出现此错误: hello.go:9: error declared and not used I was suprised at this since most language compilers just warn you about unused variables but still compile.我对此感到惊讶,因为大多数语言编译器只是警告您未使用的变量,但仍然可以编译。

Is there anyway I can get around this?无论如何我可以解决这个问题吗? I checked the documentation for the compiler and I don't see anything that would change this behaviour.我检查了编译器的文档,我没有看到任何会改变这种行为的东西。 Is there a way to just delete error so that this will compile?有没有办法只删除error以便编译?

package main

import "fmt"
import "os"

func main()
{
     fmt.Printf("Hello World\n");
     cwd, error := os.Getwd();
     fmt.Printf(cwd);
}

You could try this:你可以试试这个:

cwd, _ := os.Getwd();

but it seems like it would be better to keep the error like in Jurily's answer so you know if something went wrong.但似乎最好将错误保留在 Jurily 的答案中,以便您知道是否出现问题。

this can make development a bit of a pain.这会让开发变得有点痛苦。 sometimes i run code that has variables declared but not used (but will be used).有时我运行的代码声明了变量但未使用(但使用)。

in these cases i simple do this:在这些情况下,我简单地这样做:

fmt.Printf("%v %v %v",somevar1,somevar2,somevar3) fmt.Printf("%v %v %v",somevar1,somevar2,somevar3)

and there, they are "used".在那里,它们被“使用”。

i'd like to see a flag to the go tools that lets me suppress this error while developing.我想看到 go 工具的标志,让我在开发时抑制这个错误。

Does this work?这行得通吗?

cwd, error := os.Getwd();
if error == nil {
    fmt.Printf(cwd);
}

You can find out what the error is by importing "fmt" and using您可以通过导入“fmt”并使用来找出错误所在

cwd, err := os.Getwd();
if err != nil {
    fmt.Printf("Error from Getwd: %s\n", err)
}

What does it print?它打印什么?

I had the same situation as you.我和你有同样的情况。 From the docs :文档

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.由于这些原因,Go 不允许。

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.但是,还没有添加这样的选项,因为编译器选项不应该影响语言的语义,并且因为 Go 编译器不报告警告,只报告阻止编译的错误。

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. .... }

If you really just wanna remove the compile error, you can try something like 'a = a', or 'error = error'.如果你真的只想删除编译错误,你可以尝试像 'a = a' 或 'error = error' 之类的东西。

The arguments coming from some people here, stating that such compile errors are great because they prevent a lot of cruft are true for most situation, so you should avoid such constructs.来自这里的一些人的论点,指出这种编译错误很好,因为它们防止了很多 cruft 在大多数情况下都是正确的,所以你应该避免这样的构造。 On the other side I quite like to test, whether the code I write does actually compile!另一方面,我很喜欢测试,我写的代码是否真的可以编译! And in that case it's good, not having to remove all declared & unused variables.在这种情况下,这很好,不必删除所有声明和未使用的变量。 So use the 'a = a' construct rarely and don't leave them there!所以很少使用 'a = a' 结构,不要把它们留在那里!

You can resolve unused variable issue by following one of these two methods.您可以通过以下两种方法之一解决未使用的变量问题。

  1. By solving the error通过解决错误

    cwd, error := os.Getwd() if error !=nil{ fmt.Println(error) } cwd, 错误 := os.Getwd() 如果错误 !=nil{ fmt.Println(error) }

  2. Here we didn't need the value itself, so we ignored it with the blank identifier "_"这里我们不需要值本身,所以我们用空白标识符“_”忽略了它

    cwd, _ := os.Getwd() cwd, _ := os.Getwd()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM