ifacegen
ifacegen is a Go code generation tool that scans Go packages to automatically generate interfaces based on struct method sets. It's particularly useful for creating abstractions such as Service, Repository, or similar patterns.
Features
- Generates interfaces for structs matching configurable name patterns (e.g.,
*Service, *Repository).
- Skips specific structs or methods via
// ifacegen:skip comment.
- Includes structs explicitly marked with
// ifacegen:generate, even if their name doesn't match patterns.
- Preserves method comments in the generated interfaces.
- Resolves types using
go/types for accurate imports and signatures.
- Handles cross-package imports with proper aliasing.
- Concurrently processes multiple packages for improved performance.
- Outputs one file per package (default name:
iface_gen.go).
- Automatically formats and organizes the output with
goimports.
Installation
go install github.com/seyyedaghaei/ifacegen/cmd/ifacegen@latest
Usage
Basic Usage
ifacegen -match '*Service,*Repository' -output iface_gen.go ./...
Interactive Mode
For easy configuration, use interactive mode:
ifacegen -interactive
Advanced Examples
# Dry run to see what would be generated
ifacegen -dry-run -match '*Service' ./...
# Watch mode for development
ifacegen -watch -match '*Service' ./...
# Verbose output with progress indicators
ifacegen -verbose -match '*Service,*Repository' ./...
# Custom interface naming
ifacegen -name '{}Interface' -match '*Service' ./...
Flags
| Flag |
Description |
-match |
Comma-separated list of glob patterns to match struct names (e.g., *Service). |
-output |
Output filename used inside each package (default: iface_gen.go). |
-name |
Pattern for naming interfaces, using {} as the placeholder (default: I{}). |
-dry-run |
Show what would be generated without writing files. |
-verbose |
Enable verbose output with detailed logging. |
-watch |
Watch for file changes and regenerate automatically. |
-progress |
Show progress indicators (default: true). |
-interactive |
Start interactive configuration mode. |
-version |
Show version information. |
-help |
Show usage information. |
Skipping and Including Structs or Methods
You can exclude specific structs or methods from interface generation using the // ifacegen:skip comment:
// ifacegen:skip
type AuthService struct {
// ...
}
// ifacegen:skip
func (s *UserService) InternalLogic() {}
You can explicitly include structs (even if they don't match -match) with the // ifacegen:generate comment:
// ifacegen:generate
type Special struct {
// ...
}
Example
Given this struct:
// UserService handles user-related operations.
type UserService struct {}
func (s *UserService) CreateUser(name string) error {
return nil
}
func (s *UserService) GetUser(id int) (*User, error) {
return nil, nil
}
Running:
ifacegen -match '*Service' ./...
Generates:
// Code generated by ifacegen. DO NOT EDIT.
package yourpackage
// UserService handles user-related operations.
type IUserService interface {
CreateUser(name string) error
GetUser(id int) (*User, error)
}
CLI Features
Interactive Mode
The -interactive flag starts a guided configuration process:
- Step-by-step setup of all options
- Preview of generated command
- Confirmation before execution
Dry Run Mode
Use -dry-run to preview what would be generated:
- Shows which files would be created/updated
- Displays file size changes
- No actual files are written
Watch Mode
Use -watch for development workflows:
- Automatically regenerates when files change
- Polls for changes every 2 seconds
- Perfect for development environments
Progress Indicators
- Real-time progress updates
- Package-by-package status
- Success/failure indicators
- Disable with
-progress=false
Verbose Output
Use -verbose for detailed logging:
- Package loading information
- Generation details
- Error context
- File operation logs
Output Location
The generated file is written once per package, in the same directory as the structs, using the filename from -output (default iface_gen.go). If the content hasn't changed, the file won't be overwritten.
License
MIT © Morteza SeyyedAgahei