简体   繁体   中英

“variable declared and not used” compilation error

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

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.

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.

Does this work?

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

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?

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

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM