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).
- Formats generated output with
gofmt.
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, -m |
Comma-separated list of glob patterns to match struct names (e.g., *Service). Use --match or -m, not -match. |
--output, -o |
Output filename used inside each package (default: iface_gen.go). Use --output or -o, not -output. |
--name, -n |
Pattern for naming interfaces, using {} as the placeholder (default: I{}). Use --name or -n, not -name. |
--dry-run, -d |
Show what would be generated without writing files. |
--verbose, -V |
Enable verbose output with detailed logging. |
--watch, -w |
Watch for file changes and regenerate automatically. |
--progress, -p |
Show progress indicators (default: true). |
--interactive, -i |
Start interactive configuration mode. |
--version, -v |
Show version information. |
--help, -h |
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 {
// ...
}
You can override the interface name with // ifacegen:name:
// ifacegen:name IOAuthStateRepository
type oauthStateRepository 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 / -i 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 / -d 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 / -w 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 / -V 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.
Docs
- Development:
docs/development.md
- Breaking changes policy:
docs/breaking-changes-policy.md
- How it works:
docs/how-it-works.md
- Releases:
docs/releases.md
- Contributing:
CONTRIBUTING.md
License
MIT. See LICENSE.