interface_based

command
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 15, 2026 License: MIT Imports: 2 Imported by: 0

README

Interface-Based Example

This example demonstrates how to bind a concrete type to an interface using ProvideAs.

What it demonstrates

  • Registering a concrete implementation against an interface with gioc.ProvideAs[Logger](c, NewConsoleLogger)
  • A service that depends on the Logger interface, not a concrete type
  • The container automatically injects the concrete implementation

Code explanation

  1. Logger – an interface with a Log(message string) method
  2. ConsoleLogger – implements Logger
  3. Service – constructor takes Logger (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 ConsoleLogger for a mock in tests
  • Flexibility – change implementations without touching service code

Output

Log: Service initialized successfully

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL