
adapt
generates adapter type to allow the use of ordinary functions as the interface.
go get -u github.com/romanyx/adapt
You can pass package and interface names to generate adapter:
$ adapt io Reader
type readerFunc func([]byte) (int, error)
func (f readerFunc) Read(p []byte) (int, error) {
return f(p)
}
You also can call adapt
inside a package folder to generate adapter for some of its interfaces:
$ cd $GOPATH/src/github.com/romanyx/polluter Polluter
$ adapt Polluter
type polluterFunc func(io.Reader) error
func (f polluterFunc) Pollute(r io.Reader) error {
return f(r)
}
It comes in handy for Unit testing with table driven tests.
You can use adapt
from Vim with vim-go-adapt