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. It also patches container.go and wire.go to register the new provider.
Usage
gofasta g provider <Name>This command has no flags.
Examples
Generate a provider for a Product resource:
gofasta g provider ProductWhat It Generates
Running gofasta g provider Product creates one file and patches two:
| File | Description |
|---|---|
app/di/providers/product.go | Wire DI provider set (created) |
app/di/container.go | Adds resource fields (patched) |
app/di/wire.go | Adds ProductSet to Wire build (patched) |
Generated 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
}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