ray-x / go.nvim

G'day Nvimer, Joyful Gopher: Discover the Feature-Rich Go Plugin for Neovim
MIT License
2.06k stars 123 forks source link

Support for go generics in language server? #350

Closed rodrigc closed 1 year ago

rodrigc commented 1 year ago

Hi @ray-x , nice neovim plugin, as usual. :)

This may not be a go.nvim question, but I thought I would ask you since you are quite knowledgeable.

Do you know if any of the go language servers support Go generics?

I read this tutorial: https://go.dev/doc/tutorial/generics

and wrote this code example:

package main

import (
    "fmt"
)

// SumIntsOrFloats sums the values of map m. It supports both int64 and float64
// as types for map values.
func SumIntsOrFloats[K comparable, V int64 | float64](m map[K]V) V {
    var s V
    for _, v := range m {
        s += v
    }
    return s
}

func main() {
    // Initialize a map for the integer values
    ints := map[string]int64{
        "first":  34,
        "second": 12,
    }

    // Initialize a map for the float values
    floats := map[string]float64{
        "first":  35.98,
        "second": 26.99,
    }
    fmt.Printf("Generic Sums: %v and %v\n",
        SumIntsOrFloats[string, int64](ints),
        SumIntsOrFloats[string, float64](floats))

}

The inline language hints looked like this:

func SumIntsOrFloats[K comparable, V int64 | float64](m map[K]V) V { // W: expected '(', found '[' (and 2 more errors)

But that line is valid.

I am using gopls at this version:

golang.org/x/tools/gopls@v0.12.2 h1:s4mznBqCHVHeuTYjxGYJhsW3Wp4FYon8YAqiJlwlpTY=
ray-x commented 1 year ago

I am using 0.12.2 as well. the inline hint looks good to me

image

The error might come from another linter?

rodrigc commented 1 year ago

What does the inline hint on func SumIntsOrFloats look like for you?

ray-x commented 1 year ago
image

I do not see warnings around the generic func.

rodrigc commented 1 year ago

Ah OK, I figured it out.

I use this plugin: https://github.com/dense-analysis/ale

and the default linters that Ale uses for Go are:

   'go': ['gofmt', 'golint', 'gopls', 'govet'],

golint does not support generics, and prints out this warning: expected '(', found '[' (and 2 more errors)

So the problem is with golint, as invoked by ale