“已声明但未使用的变量”编译错误
我正在学习 Google 的新语言 Go。我只是在尝试一些东西,我注意到 如果您声明一个变量并且不对其执行任何操作,则 go 编译器(在我的例子中为 8g)将无法
编译时出现以下错误:hello.go:9: error声明但未使用
。我对此感到惊讶,因为大多数语言编译器只是警告您未使用的变量,但仍然编译。
无论如何我可以解决这个问题吗?我检查了编译器的文档,没有看到任何会改变此行为的内容。有没有办法只删除 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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
你可以尝试这个:
但似乎最好保留朱莉的答案中的错误,这样你就知道是否出了问题。
You could try this:
but it seems like it would be better to keep the error like in Jurily's answer so you know if something went wrong.
这可能会让开发变得有点痛苦。有时我运行的代码已声明但未使用变量(但将使用)。
在这些情况下,我简单地这样做:
在那里,它们被“使用”。
我希望看到 go 工具有一个标志,可以让我在开发时抑制此错误。
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:
and there, they are "used".
i'd like to see a flag to the go tools that lets me suppress this error while developing.
这有效吗?
Does this work?
来找出错误是什么?
您可以通过导入“fmt”并使用它打印什么
You can find out what the error is by importing "fmt" and using
What does it print?
我和你有同样的情况。来自文档:
I had the same situation as you. From the docs:
如果你真的只是想消除编译错误,你可以尝试类似“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!
您可以通过以下两种方法之一解决未使用变量的问题。
通过解决错误
cwd, error := os.Getwd() if error !=nil{ fmt.Println(error) }
这里我们不需要值本身,所以我们忽略它
空白标识符“_”
cwd, _ := os.Getwd()
You can resolve unused variable issue by following one of these two methods.
By solving the error
cwd, error := os.Getwd() if error !=nil{ fmt.Println(error) }
Here we didn’t need the value itself, so we ignored it with the
blank identifier "_"
cwd, _ := os.Getwd()