问题
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:
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:
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:
Does this work?
cwd, error := os.Getwd();
if error == nil {
fmt.Printf(cwd);
}
回答4:
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:
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!
回答6:
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. .... }
来源:https://stackoverflow.com/questions/1718717/variable-declared-and-not-used-compilation-error