Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate interface implementations in VS Code for Go?

In VSCode, how do I generate the implementation for an interface?

Say, I have this interface:

type ServerInterface interface {
    // Set value for a device
    SetSomethingForDeviceById(ctx echo.Context, id int64) error
}

How do I generate methods that implement it?

like image 852
kolypto Avatar asked Nov 01 '25 07:11

kolypto


1 Answers

VScode supports interface generation with the Go extension.

Here's how you do it:

First, you start with defining your struct:

type ApiServer struct {}

Now, use Ctrl-Shift-P, and find this command: "Go generate interface stubs"

command

Now type something like this: receiver name, type, interface name:

s ReceiverType package.InterfaceName

input

Hit Enter. Missing methods are generated:

package api

import "github.com/labstack/echo/v4"

// Set value for a device
func (s ApiServer) SetSomethingForDeviceById(ctx echo.Context, id int64) error {
    panic("not implemented")
}

@clément-jean added that:

this command depends on https://github.com/josharian/impl: you need to install it before being able to generate the code.

like image 84
kolypto Avatar answered Nov 03 '25 09:11

kolypto