0

Go compiler complain about unused variable and imports. So go scripts can't be run within gwan if any unused var/script is detected.

gwan provide a 404 error in this case if running ... or simply can't be start

There is a way to avoid this behavior?

Gil
  • 3,279
  • 1
  • 15
  • 25
solisoft
  • 669
  • 4
  • 12
  • It also crash g-wan when running in daemon mode :( – solisoft Jan 18 '13 at 12:17
  • Don't declare variables if you are not using them. –  Jan 19 '13 at 09:04
  • 1
    Solisoft - G-WAN did not "crash": it merely stopped because of an error in a script. You should always test your scripts by running G-WAN in a terminal **before** you fire the (blind) daemon mode. This will let you read the compiler error messages relayed by G-WAN. – Gil Jan 19 '13 at 15:33

2 Answers2

8

No really easy way exists. There's nothing like a compiler flag to turn this behavior off. I guess it's just better to pass around code which the compiler can swallow in the first place.

EDIT: C/P from the FAQ: 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.
        ....
}
Community
  • 1
  • 1
zzzz
  • 87,403
  • 16
  • 175
  • 139
-1

That's a duplicate Go language question, see:

Go: “variable declared and not used” compilation error

The Go compiler treats "unused variable" as a fatal error. This is something that all other languages treat as a warning, hence G-WAN stopping to let you fix the script error(s).

There's nothing G-WAN can do to help you here: you must use Go as expected.

Community
  • 1
  • 1
Gil
  • 3,279
  • 1
  • 15
  • 25