在使用VS Code進(jìn)行Go語言開發(fā)時(shí),生成接口實(shí)現(xiàn)是一個(gè)常見的需求。接口實(shí)現(xiàn)可以幫助我們快速生成遵循接口定義的代碼模板,提高開發(fā)效率。那么,如何在VS Code for Go中實(shí)現(xiàn)這個(gè)功能呢? 本文將為您介紹幾種實(shí)現(xiàn)接口生成的方法,幫助您更好地利用VS Code進(jìn)行Go語言開發(fā)。
問題內(nèi)容
在 vscode 中,如何生成接口的實(shí)現(xiàn)?
比如說,我有這個(gè)界面:
type ServerInterface interface { // Set value for a device SetSomethingForDeviceById(ctx echo.Context, id int64) error }
登錄后復(fù)制
如何生成實(shí)現(xiàn)它的方法?
解決方法
vscode 支持使用 go 擴(kuò)展生成界面。
具體操作方法如下:
首先,從定義結(jié)構(gòu)開始:
type apiserver struct {}
登錄后復(fù)制
現(xiàn)在,使用 ctrl-shift-p,找到此命令:“gogenerateinterfacestubs”
現(xiàn)在輸入如下內(nèi)容:接收者名稱、類型、接口名稱:
s receivertype 包.interfacename
按 enter 鍵。生成缺少的方法:
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") }
登錄后復(fù)制
@clément-jean 補(bǔ)充說:
此命令取決于https://www.php.cn/link/428b8e0c8ae876e78e551367212ae73b:您需要在生成代碼之前安裝它。