goigen
Goigen is a tool for generating interfaces from method lists
For any given type, goigen can generate an interface to which that type complies,
along with mocks for that type (if mockgen is installed)
Usage
goigen [options] DIRECTORY RECEIVER INTERFACE
goigen supports the following options:
- mockdirectory - specifies the directory mocks are written to relative to DIRECTORY
- fileprefix - specifies the prefix added to generated file names
goigen takes three arguments (options MUST come before arguments):
- DIRECTORY - the directory to search for methods in (e.g.
processor/testfiles
)
- RECEIVER - the receiver to match the interface to (e.g.
Example
or *Example
)
- INTERFACE - the name of the interface to generate (e.g.
IExample
)
Example
Consider the following go code:
//go:generate goigen . *Example ExampleInterface
package example
type Example struct{}
func (e Example) NotIncluded() {}
func (e *Example) Included() {}
running go generate
on the above code would produce the below file (named generated_exampleinterafce.go)
//generated by goigen, do not edit manually
//go:generate mockgen -source=generated_exampleinterface.go -package=example -destination=./generated_exampleinterface_mock.go
package example
type ExampleInterface interface {
Included()
}
it would also automatically run go generate on the generated file, producing the below mocks (named generated_exampleinterface_mock.go)
// Code generated by MockGen. DO NOT EDIT.
// Source: generated_iexample.go
// Package testfiles is a generated GoMock package.
package testfiles
import (
gomock "github.com/golang/mock/gomock"
reflect "reflect"
)
// MockIExample is a mock of IExample interface
type MockIExample struct {
ctrl *gomock.Controller
recorder *MockIExampleMockRecorder
}
// MockIExampleMockRecorder is the mock recorder for MockIExample
type MockIExampleMockRecorder struct {
mock *MockIExample
}
// NewMockIExample creates a new mock instance
func NewMockIExample(ctrl *gomock.Controller) *MockIExample {
mock := &MockIExample{ctrl: ctrl}
mock.recorder = &MockIExampleMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use
func (m *MockIExample) EXPECT() *MockIExampleMockRecorder {
return m.recorder
}
// Included mocks base method
func (m *MockIExample) Included() {
m.ctrl.T.Helper()
m.ctrl.Call(m, "Included")
}
// Included indicates an expected call of Included
func (mr *MockIExampleMockRecorder) Included() *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Included", reflect.TypeOf((*MockIExample)(nil).Included))
}