【变量声明但未使用】 编译错误

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

"variable declared and not used" compilation error

问题

我正在学习Google的新语言Go。我只是在尝试一些东西,我注意到如果你声明一个变量但没有对它进行任何操作,go编译器(在我的情况下是8g)会编译失败,并显示错误信息:hello.go:9: error declared and not used。我对此感到惊讶,因为大多数语言编译器只会警告你有未使用的变量,但仍然会编译通过。

有没有办法绕过这个问题?我查看了编译器的文档,但没有找到任何可以改变这种行为的内容。有没有办法只删除error,这样就可以编译通过了?

package main

import "fmt"
import "os"

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

I am learning Google's new language 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
compile with this error: 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?

package main

import "fmt"
import "os"

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

答案1

得分: 24

你可以尝试这样做:

cwd, _ := os.Getwd();

但是似乎最好还是像Jurily的回答中那样保留错误,这样你就知道是否出现了问题。

英文:

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.

答案2

得分: 11

这可能会让开发有点麻烦。有时候我运行的代码会声明但未使用的变量(但将会被使用)。

在这些情况下,我只需这样做:

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

然后,它们就被“使用”了。

我希望能够看到一个标志,让我在开发过程中可以抑制这个错误。

英文:

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)

and there, they are "used".

i'd like to see a flag to the go tools that lets me suppress this error while developing.

答案3

得分: 4

这个工作吗?

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

Does this work?

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

答案4

得分: 3

你可以通过导入 "fmt" 并使用以下代码来找出错误:

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

它会打印什么?

英文:

You can find out what the error is by importing "fmt" and using

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

What does it print?

答案5

得分: 2

我和你有相同的情况。从文档中可以看到:

> 我能停止关于未使用变量/导入的投诉吗?
>
> 未使用变量的存在可能表示一个bug,而未使用的导入只会减慢编译速度。如果在代码树中累积了足够多的未使用导入,编译速度会变得非常慢。出于这些原因,Go语言不允许这两种情况发生。
>
> 在开发代码时,临时创建这些情况是很常见的,但在程序编译之前必须将它们删除可能会很烦人。
>
> 有些人要求添加一个编译器选项来关闭这些检查,或者至少将其降级为警告。尽管有这样的要求,但并没有添加这样的选项,因为编译器选项不应该影响语言的语义,并且Go编译器只报告阻止编译的错误,而不是警告。
>
> 没有警告的原因有两个。首先,如果值得抱怨,就值得在代码中修复。(如果不值得修复,就不值得提及。)其次,编译器生成警告会鼓励实现在可能导致编译变得嘈杂的弱情况下发出警告,从而掩盖了应该修复的真正错误。
>
> 不过,解决这个问题很容易。在开发过程中,可以使用空白标识符来保留未使用的内容。
>
> import "unused"
>
> // 通过引用包中的一个项,这个声明将导入标记为已使用。
> var _ = unused.Item // TODO: 提交之前删除!
>
> func main() {
> debugData := debug.Profile()
> _ = debugData // 仅在调试期间使用。
> ....
> }

英文:

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

答案6

得分: 1

如果你只是想要解决编译错误,你可以尝试像'a = a'或'error = error'这样的方法。

一些人在这里提出的论点是,这种编译错误很好,因为它们可以防止很多冗余代码,对于大多数情况来说是正确的,所以你应该避免使用这样的结构。另一方面,我很喜欢测试我编写的代码是否真的可以编译!在这种情况下,不需要删除所有已声明但未使用的变量是很好的。所以很少使用'a = a'这样的结构,不要把它们留在那里!

英文:

If you really just wanna remove the compile error, you can try something like 'a = a', or '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. 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!

答案7

得分: 0

你可以通过以下两种方法解决未使用变量的问题。

  1. 通过解决错误

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

  2. 在这里,我们不需要这个值本身,所以我们用空白标识符“_”忽略它

    cwd, _ := os.Getwd()

英文:

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

  2. Here we didn’t need the value itself, so we ignored it with the
    blank identifier "_"

    cwd, _ := os.Getwd()

huangapple
  • 本文由 发表于 2009年11月12日 07:05:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/1718717.html
匿名

发表评论

匿名网友

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

确定