As of gopls v0.10.1, the go to implementation feature of the LSP supports finding all of the types which implement an interface via the "Go to Implementation" feature in VS Code and Neovim etc.

This is a feature request to add the same support for named function types, and anonymous functions.

Current behaviour

https://user-images.githubusercontent.com/1029947/200022424-960e463d-7581-47af-ac34-113a2b894fbf.mov

Go also allows you to define function types and also accept functions as a parameter. This is seen in the standard library, e.g. https://pkg.go.dev/net/http#HandlerFunc and https://pkg.go.dev/strings#IndexFunc but here's a simple example.

type FunctionType func(s string, i int)

Any function that has the same signature implements that function type.

func ImplementationOfFunctionType1(s string, i int) {
}

func ImplementationOfFunctionType2(s string, i int) {
}

So, you can use it like this:

func TestFunctionType(f FunctionType) {
    f("s", 0)
}

The current behaviour of gopls does not allow you to go to the implementation of f from within the function body of the TestFunctionType function.

https://user-images.githubusercontent.com/1029947/200023200-f1bb4f6e-17d5-4806-a0f4-ebfe348f95e3.mov

And this same behaviour is true of anonymous functions:

https://user-images.githubusercontent.com/1029947/200023283-dfb77b4b-f5d4-45b1-9306-569008d5a0bb.mov

Expected behavior

It should be possible to go the functions which implement the signature, as per this example.

https://user-images.githubusercontent.com/1029947/200023352-e230dae8-ce32-48bc-809e-8db7ca622c1b.mov

I've also tested the same behaviour with my modified version of the gopls Language Server running with Neovim.

Implementation

I've got an implementation of the functionality to contribute.

Comment From: findleyr

Hi, if you already have an implementation for this, we can take a look. As long as it is not too invasive, this is a feature we'd happily accept. Thanks!

Comment From: a-h

Thanks @findleyr - I've added a PR over here: https://github.com/golang/tools/pull/412

I've tried to keep the new features out of the way as much as possible.

Comment From: gopherbot

Change https://go.dev/cl/619195 mentions this issue: gopls/internal/golang: allow Implementation on any expression

Comment From: gopherbot

Change https://go.dev/cl/654556 mentions this issue: gopls/internal/golang: Implementations for func types

Comment From: gopherbot

Change https://go.dev/cl/655175 mentions this issue: gopls/internal/util/fingerprint: split from cache/methodsets

Comment From: adonovan

This was implemented in the local (same-package) case by CL 654556. The global case remains unaddressed.