php editor Baicao introduces you how to configure VSCode to display unnecessary generics in Go. With the development of the Go language, generics have become the focus of developers. However, during coding, sometimes we over-specify generics, resulting in code that is verbose and difficult to maintain. To solve this problem, VSCode provides some configuration options that can help us display unnecessary generics in the editor, making the code more concise and readable. The following will introduce you in detail how to configure VSCode to display unnecessary generics, making your Go development more efficient and convenient.
In the code below
package main import "fmt" func test[A, B any](a A, b B) { fmt.Printf("a: %v, b: %v", a, b) } func main() { test[string, int]("test", 1) }
Explicit type specification when calling test methods is unnecessary and over-specification. Calling test("test", 1")
is sufficient because the type can be inferred from the parameters.
Is it possible to configure VSCode to indicate this? Or is there a linter that can report this issue? I somehow remember seeing VSCode displaying unnecessary type specifications as gray text, but either I messed up my configuration or this functionality is gone.
This is very helpful for more advanced cases, especially since type inference in go is steadily improving and code written for older go versions may be simplified.
Set according to documentation
"gopls": { "ui.diagnostic.analyses": { "infertypeargs": true } }
Should result in a visual indication of unused types. But that didn't come up for me.
Currently, this analyzer can only be used via code manipulation within unnecessary type parameters:
x/tools/gopls: infertypeargs no longer generates diagnostic messages #63821 Tracking lack of diagnostic messages. After this issue is resolved, the diagnostic messages should reappear in VS Code.
infertypeargs
Enabled by default, so no configuration is required.
The above is the detailed content of How to configure vscode to show unnecessary (over-specified) generics in go?. For more information, please follow other related articles on the PHP Chinese website!