https://github.com/sysulq/struct2interface
Dev helper tool that generates a Golang interface by inspecting the structure methods of existing .go files.
https://github.com/sysulq/struct2interface
Last synced: 9 months ago
JSON representation
Dev helper tool that generates a Golang interface by inspecting the structure methods of existing .go files.
- Host: GitHub
- URL: https://github.com/sysulq/struct2interface
- Owner: sysulq
- License: mit
- Created: 2022-02-28T02:33:02.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2025-03-25T02:23:20.000Z (over 1 year ago)
- Last Synced: 2025-03-30T08:32:12.643Z (about 1 year ago)
- Language: Go
- Homepage: https://pkg.go.dev/github.com/hnlq715/struct2interface
- Size: 159 KB
- Stars: 10
- Watchers: 3
- Forks: 7
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# struct2interface
[](https://github.com/sysulq/struct2interface/actions/workflows/go.yml)
This is a development helper program that generates a Golang interface by inspecting
the structure methods of an existing `.go` file. The primary use case is to generate
interfaces for mockery, so that mockery can generate mocks from those interfaces. This
makes unit testing easier.
## Install
```
go install github.com/sysulq/struct2interface/cmd/struct2interface@latest
```
## Usage
Here is the help output of struct2interface:
```
$ struct2interface --help
Usage:
struct2interface [flags]
Flags:
-d, --dir string Go source file dir to read (default ".")
-h, --help help for struct2interface
```
As an example, let's say you wanted to generate an interface for the Method structure
in this sample code:
```
package testdata
// Method describes the code and documentation
// tied into a method
type Method struct {
Code string
Docs []string
}
// Lines return a []string consisting of
// the documentation and code appended
// in chronological order
func (m *Method) Lines() []string {
var lines []string
lines = append(lines, m.Docs...)
lines = append(lines, m.Code)
return lines
}
```
The struct2interface helper program can generate this interface for you:
```
$ struct2interface -d testdata
struct2interface: testdata: wrote testdata/interface_Method.go
struct2interface: testdata: wrote testdata/interface_Method1.go
```