本地Golang升级到1.18后,发现原本写的一些代码在Goland中出现了一些红色的波浪线,将鼠标移到错误提示上,有如下的显示:
Cannot use 'err' (type error) as the type any
conn, err := listener.Accept()
if err != nil {
panic(err) // 1.18后Goland会报错,hong
}
buf := make([]byte, 1024)
Golang 1.18 版本开始引入any
类型可以替代空接口 interface{}
var err any = "异常报错"
panic(err)
//或者
panic(any("异常报错"))
代码在执行的时候并没有报错,而仅仅是在Goland中会有这个错误提示呢?
有没有一种可能是,Goland版本低了对Go 1.18的新特性支持的不是很友好呢?
点击Release Nodes进入发布日志
发现确实有这么一条更新日志:
Go 1.18 Generics: Type any isn't properly supported: Cannot use ... (type string) as the type any
那就是说明在Go 1.18版本之后,Goland的2021.3.3之前的版本都会有这个BUG。
以上两种方式都可以解决golang1.18版本后,goland中panic(err) 对string类型检查报错的问题。
但是最好还是去升级一下Goland,这样对代码会更友好一些。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。