今天给项目加上了golangci检测,结果运行gosimple的时候报了这样一个问题:

xxx.go:289:10: S1034: assigning the result of this type assertion to a variable (switch err := err.(type)) could eliminate the following type assertions:xxx.go:291:14 (gosimple)switch err.(type) {^

这个问题对应的程序是这样的:

switch err.(type) {case *fatalException:msg := err.(*fatalException).Messagedefault://...}

gosimple是用来检测代码是不是有可以简化的地方的,可是看这段代码,感觉没啥简化的空间了啊。。。

于是认真翻译了一下这句话:

将类型断言的结果分配给变量,可以去除后续的断言

乍一看没懂,可是仔细它给的信息,发现它告诉你了,就是这样改:

switch err := err.(type)

纳尼?还有这种写法??靠不靠谱啊。。。

于是我写了段代码,验证下:

package mainimport "fmt"func main() {var a interface{}a = "123"switch a := a.(type) {case int:fmt.Printf("int %d", a)case string:fmt.Printf("string %s", a)default:fmt.Println("unknown")}
}

运行结果是这样的:

到这我就明白了,把类型判断的结果分配给变量,这个变量就变成了类型转换后的结果,这样在对应case里处理的时候,就不用再来一次类型转换了,比如我最开始给出的那段代码,改成下面这样就可以了:

switch err := err.(type) {case *fatalException:msg := err.Messagedefault://...}

这么一看,确实是简化了,哈哈,还是怪自己才疏学浅了

assigning the result of this type assertion to a variable could eliminate the followin assertion解决相关推荐

  1. [原创]java WEB学习笔记58:Struts2学习之路---Result 详解 type属性,通配符映射

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  2. QT error: assigning to ‘QListwidget *‘ from incompatible type ‘QListWidget *‘

    \class_sax.cpp:10: error: assigning to 'QListwidget *' from incompatible type 'QListWidget *' QListw ...

  3. variable ‘‘ of type ‘‘ referenced from scope ‘‘, but it is not defined 异常解决方法

    variable '' of type '' referenced from scope '', but it is not defined 异常解决方法 参考文章: (1)variable '' o ...

  4. Eclipse正确配置Tomcat之后仍然报错Type Target runtime Apache Tomcat v8.0 is not defined解决方式

    Eclipse正确配置Tomcat之后仍然报错Type Target runtime Apache Tomcat v8.0 is not defined解决方式 虽然项目能正常运行,但是看着这个红叉叉 ...

  5. error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 解决方法

    error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 解决方法 参考文章: ...

  6. iOS Assigning to 'idXXXDelegate' from incompatible type 'BViewController *__strong'

    在使用代理的时候, BViewController *BVC = [[BViewController alloc]init];self.delegate = BVC; 出现这样的警告Assigning ...

  7. java.sql.SQLException: Operation not allowed for a result set of type ResultSet.TYPE_FORWARD_ONLY报错

    用结果集ResultSet接受数据库中的数据,想要指针复位rs.beforeFirst();却发生以下报错(这里的rs是ResultSet定义的对象,接受车传来的数据) 只需要找到车给结果集传值的语句 ...

  8. ubuntu运行docker报错:invalid mount config for type “bind“: field Source must not be empty.(没解决)

    docker: Error response from daemon: invalid mount config for type "bind": field Source mus ...

  9. 【Python】cv2.error: ... (-215:Assertion failed) ssize.empty() in function ‘cv::resize’ 的解决方法

    遇到了奇怪的错误: cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\imgproc\src\resize.cpp:4 ...

最新文章

  1. 陶哲轩实分析 例 1.2.12 洛必达法则使用注意事项
  2. 整数转换为罗马数字 Integer to Roman
  3. Mybatis通用Mapper
  4. Python多线程的两种实现方式
  5. 无向图缩点:tarjan点双与边双缩点(模板)
  6. 覆盖索引与联合索引_浅析MySQL的索引覆盖和索引下推
  7. 表达式求值Spring.Expressions
  8. linux proc文件 write的原子性,linux - Linux中writev()系统调用的原子性 - 堆栈内存溢出...
  9. BZOJ 1083: [SCOI2005]繁忙的都市【Kruscal最小生成树裸题】
  10. C++ 常用设计模式
  11. 【华为云技术分享】ARM体系结构基础(1)
  12. 一次线上Nginx出问题排错经历
  13. Total Commander如何设置自定义快捷键在当前目录打开ConEmu
  14. 如何从github下载并运行angular项目
  15. HTML5代码雨程序
  16. 求单链表的交集和并集
  17. 服务器存储系统交付清单,附件三 软硬件交付清单(1).docx
  18. 服务器性能巡检,linux服务器巡检性能与Java 进程脚本
  19. 使用MathType编辑公式时,删除键backspace和delete不好用,解决办法
  20. 超简单的React项目打包后部署到服务器上

热门文章

  1. 微信公众平台接口程序搜索音乐
  2. 未来科学技十幻想画计算机,人教版美术教案第七册(全册)
  3. 排序刷默认值sql脚本
  4. 微信小程序:用户点击跳转付款码界面
  5. python 基础一
  6. Android系统 linux内核按键驱动开发
  7. 2021-10-01 REDIS全网最超级详细
  8. Shell 小脚本集合
  9. Mac如何安装第三方新字体?Mac字体安装图文教程
  10. C++中常见的两种二义性问题及其解决方式