-
Hi gophers, Is there a way in vscode-go to automatically generate functions for a type in order for it to implement an interface? For example: type Cow interface {
Moo() string
Weight() float64
}
type Fjall struct{} ~~ magic ~~ // Auto-generated, Fjall now implements Cow.
func (f Fjall) Moo string {}
func (f Fjall) Weight float64 {} |
Beta Was this translation helpful? Give feedback.
Answered by
xiebruce
Jun 28, 2023
Replies: 1 comment 1 reply
-
use impl, first install it go install github.com/josharian/impl@latest Assume that you have a package main
type Cow interface {
Moo() string
Weight() float64
}
type Fjall struct{}
func main() {
} In terminal, 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 impl "f Fjall" hash.Hash will output func (f Fjall) Write(p []byte) (n int, err error) {
panic("not implemented") // TODO: Implement
}
// Sum appends the current hash to b and returns the resulting slice.
// It does not change the underlying hash state.
func (f Fjall) Sum(b []byte) []byte {
panic("not implemented") // TODO: Implement
}
// Reset resets the Hash to its initial state.
func (f Fjall) Reset() {
panic("not implemented") // TODO: Implement
}
// Size returns the number of bytes Sum will return.
func (f Fjall) Size() int {
panic("not implemented") // TODO: Implement
}
// BlockSize returns the hash's underlying block size.
// The Write method must be able to accept any amount
// of data, but it may operate more efficiently if all writes
// are a multiple of the block size.
func (f Fjall) BlockSize() int {
panic("not implemented") // TODO: Implement
} |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
fluhus
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
use impl, first install it
Assume that you have a
main.go
, the code is as belowIn terminal,
cd
to the directory where themain.go
locatedcd /path/to/dir/
Use the following command
impl "f Fjall" Cow
it will output these code
If the interface is from standard library, you don't need to
cd
to the directory, for exampleimpl "f Fjall" hash.Hash
will output