drcx71276 2013-01-18 10:37
浏览 5
已采纳

Go和Gwan的未使用变量

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?

  • 写回答

2条回答 默认 最新

  • dongqian6234 2013-01-18 10:43
    关注

    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.
            ....
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘