Skip to content

Creating function stubs for a type to implement an interface #2768

Answered by xiebruce
fluhus asked this question in Help
Discussion options

You must be logged in to vote

use impl, first install it

go install github.com/josharian/impl@latest

Assume that you have a main.go, the code is as below

package main

type Cow interface {
	Moo() string
	Weight() float64
}
type Fjall struct{}

func main() {

}

In terminal, cd to the directory where the main.go located

cd /path/to/dir/

Use the following command

impl "f Fjall" Cow

it will output these code

func (f Fjall) Moo() string {
        panic("not implemented") // TODO: Implement
}

func (f Fjall) Weight() float64 {
        panic("not implemented") // TODO: Implement
}

If the interface is from standard library, you don't need to cd to the directory, for example

impl "f Fjall" hash.Hash

will output

func (f Fjall) W…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@fluhus
Comment options

Answer selected by fluhus
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
2 participants