gofasta g provider
Generates a Google Wire provider set file that defines how a resource’s dependencies are wired together. The provider set binds interfaces to implementations for the repository, service, and controller layers.
Usage
gofasta g provider <ResourceName> [flags]Flags
| Flag | Short | Default | Description |
|---|---|---|---|
--layers | -l | all | Comma-separated list of layers to include. Options: repository, service, controller |
Examples
Generate a provider with all layers:
gofasta g provider ProductGenerate a provider for only the repository and service layers:
gofasta g provider Product --layers repository,serviceWhat It Generates
Running gofasta g provider Product creates one file:
app/di/providers/product.goGenerated Code
// app/di/providers/product.go
package providers
import (
"github.com/google/wire"
"myapp/app/repositories"
repoInterfaces "myapp/app/repositories/interfaces"
"myapp/app/services"
svcInterfaces "myapp/app/services/interfaces"
"myapp/app/rest/controllers"
)
var ProductSet = wire.NewSet(
// Repository
repositories.NewProductRepository,
wire.Bind(new(repoInterfaces.ProductRepository), new(*repositories.ProductRepositoryImpl)),
// Service
services.NewProductService,
wire.Bind(new(svcInterfaces.ProductService), new(*services.ProductServiceImpl)),
// Controller
controllers.NewProductController,
)How Wire Providers Work
Each provider set uses three Wire constructs:
| Construct | Purpose |
|---|---|
Constructor function (e.g., NewProductRepository) | Tells Wire how to create an instance |
wire.Bind(interface, implementation) | Tells Wire which concrete type satisfies an interface |
wire.NewSet(...) | Groups related providers into a reusable set |
The provider set is referenced in app/di/wire.go:
//go:build wireinject
package di
import (
"github.com/google/wire"
"myapp/app/di/providers"
)
func InitializeContainer() (*Container, error) {
wire.Build(
providers.UserSet,
providers.AuthSet,
providers.ProductSet, // Added by scaffold or manually
wire.Struct(new(Container), "*"),
)
return nil, nil
}Registration Steps
When generating a provider standalone (not via scaffold), you need to manually:
- Add the provider set to
wire.Build()inapp/di/wire.go - Add the corresponding fields to the
Containerstruct inapp/di/container.go - Run
gofasta wireto regeneratewire_gen.go
When using gofasta g scaffold, all three steps are handled automatically.
Related
- gofasta wire — regenerate Wire DI code after adding providers
- gofasta g scaffold — generate all layers with automatic provider registration
- gofasta g repository — generate the repository bound in this provider
- gofasta g service — generate the service bound in this provider
Last updated on