Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.Sign up
x/tools/gopls: generate method stubs for a given interface #37537
It would be great if Gopls implemented a way where a user:
See this video for a demonstration: https://vimeo.com/394345925
A common feature that Go and other typed languages have is to generate method stubs for an interface that a type wants to implement.
For most languages, the concrete type must declare what interface it intends to implement. This makes it easier for tools to generate those stubs.
For an example, take a look at this screenshot from TypeScript in VSCode:
In Go, it is impossible to know that because interfaces are implicitly implemented.
Therefore, generating method stubs require a two step process:
Therefore, I wrote a tool that that can do both of these features: https://github.com/marwan-at-work/impl
Here's a video of how I integrated the tool with VSCode locally to demonstrate the same feature working for Go: https://vimeo.com/394345925
However, the tool uses go/packages directly and is obviously a standalone that doesn't leverage gopls with its cache and environment. Therefore, I suggest we make this a feature within gopls.
If this is something worth including, I'd be very happy to port the work I did into gopls (with a little help).
Unfortunately, the LSP protocol does not have an explicit message for "generating method stubs", and so we a few options were brought up in slack that I want to highlight here:
PS. Note that this tool used to exist in GOPATH mode using https://github.com/josharian/impl but as far as I know, it is not modules aware and lacked a number of features such as "listing available interfaces" and "automatically adding import paths"
Another possibility would be to add it as a quick fix to the
type checking error.
stanza if needed.
@ianthehat Would it be possible to expand it to wherever a concrete type is being casted as an interface? For example:
Similar to rename, it should only do it if the concrete type was defined within the workspace/module boundaries.
* Enable Codespaces - add default Go files Codespaces allows us to use a remote vscode environment embedded in the browser in GitHub This commit adds the pre-built container configuration for Go. We can customise this configuration as we wish in future commits.  https://github.com/features/codespaces/  https://docs.github.com/en/github/developing-online-with-codespaces/configuring-codespaces-for-your-project#using-a-pre-built-container-configuration  https://github.com/microsoft/vscode-dev-containers/tree/master/containers/go * Suppress/fix devcontainer Dockerfile warnings These were hadolint warnings, and there were three of them, all on .devcontainer/Dockerfile#L17: DL3003 Use WORKDIR to switch to a directory DL3008 Pin versions in apt get install. Instead of `apt-get install <package>` use `apt-get install <package>=<version>` DL3015 Avoid additional packages by specifying `--no-install-recommends` Suppressed the warnings apart from the DL3015 one, which was easy to fix. Not fixing the other warnings (for now anyway) as this Dockerfile is code that has been lifted and shifted from the `.devcontainer/Dockerfile` in: https://github.com/microsoft/vscode-dev-containers/tree/master/containers/go * Update module settings to "on" as we use modules See: https://dev.to/maelvls/why-is-go111module-everywhere-and-everything-about-go-modules-24k * Add shellcheck vscode extension to devcontainer https://www.shellcheck.net/ https://marketplace.visualstudio.com/items?itemName=timonwong.shellcheck * Use vscode go language server on devcontainer https://github.com/golang/vscode-go/blob/master/docs/gopls.md * Stop installing go dependencies on devcontainer There is no need to install them as we are using go modules - see: microsoft/vscode-go#2836 * Set vscode editor go tabsize to correct size of 8 See: microsoft/vscode-go#2479 (comment) * Use golangci-lint for vscode devcontainer linting https://github.com/golangci/golangci-lint * No longer install Guru on devcontainer It does not support Go modules: https://github.com/golang/vscode-go/blob/master/docs/tools.md#guru * Remove unnecessary gorename from devcontainer Not needed when using go modules and gopls language server: https://github.com/golang/vscode-go/blob/master/docs/tools.md#gorename * Remove unnecessary godoctor from devcontainer It is not needed when using go modules and the gopls language server: https://github.com/golang/vscode-go/blob/master/docs/tools.md#godoctor * Remove unnecessary goimports from devcontainer Imports are instead handled by the gopls language server: golang/go#33587 (comment) * Remove unnecessary golint from devcontainer Not needed as we are using golangci-lint * Remove unnecessary gotests from devcontainer gotests autogenerates table tests boilerplate code. We don't need this for this project. * Remove unnecessary goplay module from devcontainer We don't need this functionality: https://github.com/haya14busa/goplay/ * Remove unnecessary gometalinter from devcontainer Not needed as we are using golangci-lint for linting. * Remove unnecessary impl module from devcontainer It does not have support for go modules: golang/go#37537 * Remove unnecessary fillstruct from devcontainer fillstruct fills a struct literal with default values This functionality is now available in the gopls language server  https://github.com/davidrjenni/reftools/tree/master/cmd/fillstruct  golang/go#37576 (comment) * Add comment to explain why gopkgs is still needed It is still needed even with gopls: microsoft/vscode-go#3050 (comment) * Fix installation of golangci-lint on devcontainer Prior to this commit, vscode was saying "Analysis tools missing" and prompting to install it. Fix was to install golangci-lint via go get, as per: golangci/golangci-lint#1037 (comment) * Remove unnecessary gogetdoc from devcontainer Similar functionality is available in the gopls language server: fatih/vim-go#2808 (comment) * Remove unnecessary revive linter from devcontainer We are using golangci-lint for linting, so no need for revive. * Remove unnecessary go-tools from devcontainer go-tools primarily contains staticcheck, which we don't need as we are using golangci-lint for linting. * Output go version after building devcontainer Doing this just to provide assurance that the container has started successfully, after building. * Move settings which are not container-specific The guideline for`devcontainer.json` settings is that they should only contain settings which _must_ be be changed in a container (e.g. absolute paths). Moved the other settings to `.vscode/settings.json` so that they are more visible, and easier to use when working locally (i.e. not in a container).  microsoft/vscode-remote-release#874 (comment) * Up tests timeout as tests are slower on container The tests seem to run much slower (c. 100 seconds compared to c. 50 seconds) when running on a remote container, compared to locally. Will investigate to see if this is the same when running on Codespaces. If the tests are taking 100 seconds we may need to create an issue to speed them up. * Add recommended vscode go settings As per the recommendations at: https://github.com/golang/tools/blob/master/gopls/doc/vscode.md * Set dark colour theme for vscode in devcontainer * Set devcontainer vscode to autosave after 500ms Adding these settings to the devcontainer settings file rather than the vscode settings file, as we want them to apply at the machine level. https://code.visualstudio.com/docs/editor/codebasics#_save-auto-save