如何在使用Go 1.18beta编写代码时,在VS Code(gopls)中禁用错误提示?

huangapple go评论75阅读模式
英文:

How to disable errors in VS Code (gopls) when writing code in go1.18beta?

问题

想要使用go1.18beta版本的泛型特性来在一个中心函数中处理错误。

错误处理函数示例:

func HandleError[T any](t T, err error) T {
	if err != nil {
        panic(err)
	}
	return t
}

第一个抛出的错误是:
expected '(', found '['

第二个抛出的错误是:
not enough arguments in call to HandleError (compiler) (WrongArgCount)

我知道 gopls 是基于 go1.17 的,这就是为什么会出现这些错误,但是否有办法禁用这些错误,就像在 TypeScript 中那样?

英文:

Want to use go1.18beta for its generics feature to handle errors in a central function.

Error handling function example

func HandleError[T any](t T, err error) T {
	if err != nil {
        panic(err)
	}
	return t
}

The first error thrown is

expected '(', found '['s

The second error thrown is

not enough arguments in call to HandleError (compiler) (WrongArgCount)

I am aware that gopls is of go1.17 and that's why it's throwing errors but is there any way to disable these errors as we do in typescript?

答案1

得分: 1

你可以按照这里描述的方式,使用1.18版本的Go构建gopls:
https://github.com/golang/tools/blob/master/gopls/doc/advanced.md#working-with-generic-code

英文:

You can build a gopls with 1.18 go as described here:
https://github.com/golang/tools/blob/master/gopls/doc/advanced.md#working-with-generic-code

huangapple
  • 本文由 发表于 2022年1月10日 17:49:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/70650453.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定