禁止Golang编译时“imported and not used”和“declared and not used”两种报错 原 荐

栏目: Go · 发布时间: 5年前

禁止Golang编译时“imported and not used”和“declared and not used”两种报错

肖特基在漏电

发布于 昨天 16:26

字数 943

阅读 3

收藏 0

Bootstrap Bash SWT Sym Go

面试:你懂什么是分布式系统吗?Redis分布式锁都不会?>>> 禁止Golang编译时“imported and not used”和“declared and not used”两种报错 原 荐

Go语言在代码规范之严格是有目共睹的,引用未使用的头文件会报“imported and not used”错误,定义未使用的变量会报“declared and not used”错误。因为golang编译器只有错误没有报警,所以代码中如果有此类问题,编译就会停止。

Golang不提供语法宽容的debug模式,也就是说,无论是什么时候,Go代码都必须保持干干净净的状态。

随手注释部分变量是编码阶段的一个常用手段,Golang的编译器每每跳出来,让你把变量的定义和相关头文件的引用也通通删掉。有时候临时注释一段代码会产生一堆未引用和未定义错误,而为了解决这些错误往往又会把代码被改得面目全非。对于debug阶段的 程序员 来讲,有一个管得太宽的编译器是一件痛苦的事情。

好在Golang是开源的,弄一个不太折腾的Golang编译器并不困难。

修改源码

首先下载golang代码:

$ git clone https://github.com/golang/go

拉到最新分支,现在最新的版本号是1.12.5:

$ cd go
$ git checkout -b debug-1.12.5  go 1.12.5

未引用头文件和未定义变量错误相关的代码在下面4个文件中:

  • src/cmd/compile/internal/gc/main.go
  • src/cmd/compile/internal/gc/subr.go
  • src/cmd/compile/internal/gc/swt.go
  • src/cmd/compile/internal/gc/walk.go

用 if false {} 把相关代码注释掉。

具体内容可以看这个patch:

diff --git a/src/cmd/compile/internal/gc/main.go b/src/cmd/compile/internal/gc/main.go
index 98ff2a3d27..2bb2e6fe37 100644
--- a/src/cmd/compile/internal/gc/main.go
+++ b/src/cmd/compile/internal/gc/main.go
@@ -1246,11 +1246,13 @@ func pkgnotused(lineno src.XPos, path string, name string) {
 	if i := strings.LastIndex(elem, "/"); i >= 0 {
 		elem = elem[i+1:]
 	}
+	if false {
 	if name == "" || elem == name {
 		yyerrorl(lineno, "imported and not used: %q", path)
 	} else {
 		yyerrorl(lineno, "imported and not used: %q as %s", path, name)
 	}
+	}
 }
 
 func mkpackage(pkgname string) {
diff --git a/src/cmd/compile/internal/gc/subr.go b/src/cmd/compile/internal/gc/subr.go
index 2a976dc4f0..be662c3df8 100644
--- a/src/cmd/compile/internal/gc/subr.go
+++ b/src/cmd/compile/internal/gc/subr.go
@@ -291,10 +291,12 @@ func importdot(opkg *types.Pkg, pack *Node) {
 		n++
 	}
 
+	if false {
 	if n == 0 {
 		// can't possibly be used - there were no symbols
 		yyerrorl(pack.Pos, "imported and not used: %q", opkg.Path)
 	}
+	}
 }
 
 func nod(op Op, nleft, nright *Node) *Node {
diff --git a/src/cmd/compile/internal/gc/swt.go b/src/cmd/compile/internal/gc/swt.go
index cc9a8f8b2c..6f27447bb0 100644
--- a/src/cmd/compile/internal/gc/swt.go
+++ b/src/cmd/compile/internal/gc/swt.go
@@ -70,12 +70,14 @@ func typecheckswitch(n *Node) {
 		if t != nil && !t.IsInterface() {
 			yyerrorl(n.Pos, "cannot type switch on non-interface value %L", n.Left.Right)
 		}
+		if false {
 		if v := n.Left.Left; v != nil && !v.isBlank() && n.List.Len() == 0 {
 			// We don't actually declare the type switch's guarded
 			// declaration itself. So if there are no cases, we
 			// won't notice that it went unused.
 			yyerrorl(v.Pos, "%v declared and not used", v.Sym)
 		}
+		}
 	} else {
 		// expression switch
 		top = ctxExpr
diff --git a/src/cmd/compile/internal/gc/walk.go b/src/cmd/compile/internal/gc/walk.go
index 509579d21f..92ad512d37 100644
--- a/src/cmd/compile/internal/gc/walk.go
+++ b/src/cmd/compile/internal/gc/walk.go
@@ -41,6 +41,7 @@ func walk(fn *Node) {
 		}
 	}
 
+	if false {
 	for _, ln := range fn.Func.Dcl {
 		if ln.Op != ONAME || (ln.Class() != PAUTO && ln.Class() != PAUTOHEAP) || ln.Sym.Name[0] == '&' || ln.Name.Used() {
 			continue
@@ -55,6 +56,7 @@ func walk(fn *Node) {
 			yyerrorl(ln.Pos, "%v declared and not used", ln.Sym)
 		}
 	}
+	}
 
 	lineno = lno
 	if nerrors != 0 {

生成debug版编译器

编译debug版Golang:

$ cd src
$ GOOS=linux GOARCH=amd64 ./bootstrap.bash

../../目录下会生成两个文件:

$ ls ../../
go/ go-linux-amd64-bootstrap/ go-linux-amd64-bootstrap.tbz

其中go-linux-amd64-bootstrap就是debug版的Golang。

在/usr/bin下面创建一个可执行文件debuggo,文件中的GOROOT指向go-linux-amd64-bootstrap目录:

#!/bin/bash
GOROOT=/PATH-TO-GOLANG/go-linux-amd64-bootstrap
${GOROOT}/bin/go $@

好了,大功告成,debug的时候执行:

$ debuggo build

编译器果然就老实了很多。

不过要发布的时候记得用官方的编译器做语法检查:

$ go build -a

© 著作权归作者所有

上一篇: 用QT仿写一个老软件,电子文档阅览播放器(esee)

下一篇: 五分钟在SAE上架设一个网络射击游戏

肖特基在漏电

粉丝 0

博文 16

码字总数 12579

作品 0

昌平

程序员

提问

相关文章 最新文章

编译go及go get进度显示

一、首先附上编译完成能显示go get进度的Go包: centos7直接解压版go:http://pan.baidu.com/s/1mik3cI8 windows7直接解压版go:http://pan.baidu.com/s/1jIaxKEE 一、编译之前修改go get进度...

tantexian

2016/08/06

97

0

禁止Golang编译时“imported and not used”和“declared and not used”两种报错 原 荐
[Go]Hello World第一次练习

版权声明:本文为博主原创文章,未经博主允许不得转载。有任何问题请邮件联系ashuo.com@qq.com https://blog.csdn.net/drdongshiye/article/details/89443215 一些简单的语法特性 上面的简单...

菜鸟腾飞

04/22

0

0

Concurrency In Golang

Yesterday, I answered a question in Quora about the concurrency model in Go. Now, I feel like I want to say more!! Concurrency in Golang is one of the most powerful features in ......

LsDimplex

2015/12/08

4

0

SonarQube Java 3.10 发布,Java 代码质量检查

SonarQube Java 3.10 发布,这是 Sonar 用来分析 Java 项目代码的插件。该版本改进了 Symbolic Execution 引擎 包含 17 条新的规则: “action” mappings should not have too many “forwa...

oschina

2016/02/06

2.9K

0

禁止Golang编译时“imported and not used”和“declared and not used”两种报错 原 荐
2. golang 基础知识--变量、类型、关键字...

第一个程序 Golang 程序由包组成,从main包开始运行 import 导入包,后面接上包所在的路径。导入后就可以用来引用 输出 2.1 变量 Go使用关键字定义变量,类型信息放在变量名后面,初始化为零...

antic

2017/10/23

0

0

没有更多内容

加载失败,请刷新页面

加载更多
springboot使用pageable获取不到分页参数

在使用pageable注解传递分页参数,controller层拿不到前端传递过来的分页参数。 不是第一次用这个注解,看了下之前写的代码,用的都是get请求……这次根据业务需求改同事的代码,同事用的是p...

ninjaFrog

25分钟前

1

0

Solidity 最新 0.5.8 中文文档发布

本文首发于深入浅出区块链社区 热烈祝贺 Solidity 最新 0.5.8 中文文档发布, 这不单是一份 Solidity 速查手册,更是一份深入以太坊智能合约开发宝典。 翻译说明 Solidity 最新 0.5.8 中文文...

Tiny熊

43分钟前

2

0

数据结构-栈

栈Stack 栈也是一种线性结构 相比数组,栈对应的操作是数组的子集 只能从一端添加元素,也只能从一端取出元素 栈是一种先进先出的数据结构 package array;public class Array<E> {...

architect刘源源

今天

9

0

禁止Golang编译时“imported and not used”和“declared and not used”两种报错 原 荐
聊聊elasticsearch的ElectMasterService

序 本文主要研究一下elasticsearch的ElectMasterService ElectMasterService elasticsearch-7.0.1/server/src/main/java/org/elasticsearch/discovery/zen/ElectMasterService.java public c......

go4it

今天

2

0

anaconda多个版本 Python 环境管理

如何同时安装多个版本的python? 应用anaconda的环境管理功能,允许同时安装多个版本的python,并且可以自由切换。 1、首先需要安装好anaconda; 2、在anaconda prompt中执行以下操作; 假设...

sharelocked

今天

3

0

没有更多内容

加载失败,请刷新页面

加载更多

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

网页艺术设计

网页艺术设计

彭钢 / 高等教育出版社 / 2006-9 / 39.00元

《网页艺术设计》将软件技术与艺术理论进行整合,注重知识性与研究性、实践性与理论性、系统性与逻辑性,全面介绍网页艺术设计的基础知识与基本实践技能,既培养学习者的网页技术应用能力,又培养学习者的艺术审美、艺术创新和研究性学习能力,使学习者在有效的课时内学习和掌握网页艺术设计的理论与实践。 《网页艺术设计》的特点是具有完整的知识结构、合理的教学设计以及立体化的教学资源。教材共分为8章,包括网页艺术......一起来看看 《网页艺术设计》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具