


How to configure vscode to show unnecessary (over-specified) generics in go?
Feb 13, 2024 am 10:50 AMphp 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.
Question content
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.
Workaround
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!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use reflection to access private fields and methods in golang

Tips for dynamically creating new functions in golang functions

The difference between performance testing and unit testing in Go language

What pitfalls should we pay attention to when designing distributed systems with Golang technology?

Golang technology libraries and tools used in machine learning

The evolution of golang function naming convention

The role of Golang technology in mobile IoT development

Can golang variable parameters be used for function return values?
