关闭golang 的 variable declared but not used和package

觉得golang的 variable declared but not used 和 package imported but not used 在调试代码的时候很不方便!!!!!! 这么多语言中只有golang会有变量不使用, 包引用不用报错的!

有没有方法去掉这两个验证? 再上生产的时候开启验证再改代码? 

方法: 修改golang源码, 将error错误改成warn错误(在eclipse上都会显示红色错误, 但warn不影响编译), 重新编译源码.

1 解决:declared but not used 
修改go/src/cmd/gc/walk.c
   
for(l=fn->dcl; l; l=l->next) {
         if(l->n->op != ONAME || (l->n->class&~PHEAP) != PAUTO || l->n->sym->name[0] == '&' || l->n->used)
              continue;
         if(l->n->defn && l->n->defn->op == OTYPESW) {
              if(l->n->defn->left->used)
                   continue;
              lineno = l->n->defn->left->lineno;
              warn("%S declared and not used", l->n->sym);
              l->n->defn->left->used = 1; // suppress repeats
         } else {
              lineno = l->n->lineno;
              // 此处修改
              // yyerror("%S declared and not used, l->n->sym); // life
         }
    }  

2 解决imported but not used:
修改:go/src/cmd/gc/lex.c
if ( s -> def -> op  ==  OPACK )   {
            // throw away top-level package name leftover
           // from previous file.
         // leave s->block set to cause redeclaration
           // errors if a conflicting top-level name is
           // introduced by a different file.
     if(!s->def->used && !nsyntaxerrors) {
        lineno = s->def->lineno;
        warn("imported and not used: \"%Z\"" , s->def->pkg->path);
       }
       s->def = N;
             continue;
  }

本文之前发布在google group golang-china上: https://groups.google.com/forum/?fromgroups#!topic/golang-china/W0WMC__2NO8

转载于:https://my.oschina.net/lifephp/blog/185653

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值