This example demonstrates how to bind a concrete type to an interface using ProvideAs.
- Registering a concrete implementation against an interface with
gioc.ProvideAs[Logger](c, NewConsoleLogger) - A service that depends on the
Loggerinterface, not a concrete type - The container automatically injects the concrete implementation
Logger– an interface with aLog(message string)methodConsoleLogger– implementsLoggerService– constructor takesLogger(the interface)
ProvideAs[Logger] tells the container: "when someone needs a Logger, use NewConsoleLogger". When Service is resolved, its Logger parameter is matched by type to the interface binding.
This pattern enables:
- Decoupling – the service doesn't know which logger implementation it uses
- Testability – swap
ConsoleLoggerfor a mock in tests - Flexibility – change implementations without touching service code
Log: Service initialized successfully