Table of Contents
Question content
Workaround
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?

Feb 13, 2024 am 10:50 AM
go language

如何配置 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!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use reflection to access private fields and methods in golang How to use reflection to access private fields and methods in golang May 03, 2024 pm 12:15 PM

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

Tips for dynamically creating new functions in golang functions Tips for dynamically creating new functions in golang functions Apr 25, 2024 pm 02:39 PM

Tips for dynamically creating new functions in golang functions

The difference between performance testing and unit testing in Go language The difference between performance testing and unit testing in Go language May 08, 2024 pm 03:09 PM

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? What pitfalls should we pay attention to when designing distributed systems with Golang technology? May 07, 2024 pm 12:39 PM

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

Golang technology libraries and tools used in machine learning Golang technology libraries and tools used in machine learning May 08, 2024 pm 09:42 PM

Golang technology libraries and tools used in machine learning

The evolution of golang function naming convention The evolution of golang function naming convention May 01, 2024 pm 03:24 PM

The evolution of golang function naming convention

The role of Golang technology in mobile IoT development The role of Golang technology in mobile IoT development May 09, 2024 pm 03:51 PM

The role of Golang technology in mobile IoT development

Can golang variable parameters be used for function return values? Can golang variable parameters be used for function return values? Apr 29, 2024 am 11:33 AM

Can golang variable parameters be used for function return values?

See all articles