Go:“变量声明和未使用”编译错误 [英] Go: "variable declared and not used" compilation error
问题描述
我正在学习Google的新语言前往。我只是尝试东西,我注意到
,如果你声明一个变量,不做任何事情,go编译器( 8g 在我的情况)失败
编译时出现此错误: 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.
有没有我能解决这个问题?我检查了编译器的文档,我没有看到任何会改变这种行为。有没有办法只是删除错误
,以便这将编译?
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);
}
推荐答案
/ p>
You could try this:
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.
这篇关于Go:“变量声明和未使用”编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!