Home > Backend Development > Golang > How to configure vscode to show unnecessary (over-specified) generics in go?

How to configure vscode to show unnecessary (over-specified) generics in go?

PHPz
Release: 2024-02-13 10:50:09
forward
661 people have browsed it

如何配置 vscode 以显示 go 中不必要的(过度指定的)泛型?

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.

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)
}

Copy after login

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
        }
    }
Copy after login

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!

Related labels:
source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template