gointerface is an interface extractor for Go.
To install gointerface, just use the following command:
go install github.com/yeefea/gointerface@latestUsage of gointerface: -i string Input file or directory. By default, the program reads from stdin. -o string Output file. By default, the program writes content to stdout. -p string Rewrite the package name in the output. -private Include private methods. -t string Specify the types. Multiple types are separated by comma(,). Extract all types if not specified. For example, let's create a file example.go and define a few types as follows:
package example import ( "fmt""strings" j "encoding/json" ) typeSomeStructstruct{} /*Some block comments.*/// ToJson converts the object to json bytes.// some other commentsfunc (x*SomeStruct) ToJson(sb strings.Builder) ([]byte, error){returnj.Marshal([]string{"1", "2", "3"}) } // ComplexMethod is a complex method.func (s*SomeStruct) ComplexMethod(c, d*map[string]int) (f1, f2func( *map[string]int, *map[string]int), x*map[string]int, y*map[string]int){returnnil, nil, nil, nil } // privateMethod is a private method.func (x*SomeStruct) privateMethod(sb strings.Builder) ([]byte, error){returnj.Marshal([]string{"1", "2", "3"}) } typeIntArray []int// Desc describes the array.func (arr*IntArray) Desc(){fmt.Println(arr) }To read from a Go file and print the interfaces to stdout, use the following command:
gointerface -i example.goThe interfaces will be extracted from the source code, with the package statement, the import statements and the comments preserved.
// Code generated from gointerfacepackage example import ( j "encoding/json""fmt""strings" ) typeIIntArrayinterface{// Desc describes the array.Desc() } typeISomeStructinterface{// ComplexMethod is a complex method.ComplexMethod(c, d*map[string]int) (f1, f2func( *map[string]int, *map[string]int), x*map[string]int, y*map[string]int) /* Some block comments. */// ToJson converts the object to json bytes.// some other commentsToJson(sb strings.Builder) ([]byte, error) }By default, gointerface writes the output to stdout. To write the output to a file, use the -o option:
gointerface -i example.go -o interface.goThe program will write the output to interface.go.
Private methods, which start with a lower case letter, are not extracted by default. To extract private methods, use the -private option:
gointerface -i example.go -privateThen the private method privateMethod will included in the output interface.
typeISomeStructinterface{// ComplexMethod is a complex method.ComplexMethod(c, d*map[string]int) (f1, f2func( *map[string]int, *map[string]int), x*map[string]int, y*map[string]int) /* Some block comments. */// ToJson converts the object to json bytes.// some other commentsToJson(sb strings.Builder) ([]byte, error) // privateMethod is a private method.privateMethod(sb strings.Builder) ([]byte, error) }Let's create a new directory example and put the example.go file in it. Then create a new file example2.go in that directory. Now we have the following directory structure:
└── example ├── example.go └── example2.go Use -i option to specify the package:
gointerface -i exampleThe program will analyze all go files in the example directory and extract the interfaces.
A method can have both receivers and pointer receivers. For example, the following struct:
// example2.gopackage example typeMixedReceiverstruct{} func (r*MixedReceiver) PointerReceiver(){} func (rMixedReceiver) ValueReceiver(){}If a struct have methods with both receivers and pointer receivers, gointerface will generate two interfaces for it. One interface corresponds to the pointer receiver methods and is named I{TypeName}. The other interface corresponds to the value receiver methods and is named I{TypeName}Value. The above example will have the following two interfaces:
// Code generated from gointerfacepackage example typeIMixedReceiverinterface{PointerReceiver() } typeIMixedReceiverValueinterface{ValueReceiver() }