“已声明和未使用的变量”编译错误 [英] "variable declared and not used" compilation error
问题描述
我正在学习Google的新语言转到。我只是想尝试一些东西,我注意到
,如果你声明了一个变量并且不对它做任何事情,那么go编译器(在我的例子中是
用这个错误编译: hello.go:9:声明了错误并且未使用
。我对此感到惊讶,因为大多数语言编译器只是警告你关于未使用的变量,但仍然可以编译。
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.
有没有办法解决这个问题?我检查了编译器的文档,我没有看到任何会改变这种行为的东西。有没有办法删除 error
,这样就可以编译?
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);
}
推荐答案
您可以试试这个:
cwd, _ := os.Getwd();
但是好像保留Jurily答案中的错误会更好,所以你知道出错了。
but it seems like it would be better to keep the error like in Jurily's answer so you know if something went wrong.
这篇关于“已声明和未使用的变量”编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!