basic

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

Basic Example

This example demonstrates the core functionality of the gioc package.

What it demonstrates

  • Registering constructors with gioc.Register()
  • Automatic constructor injection (parameters resolved from the container)
  • Singleton behavior – constructors are called only once
  • Startup validation with gioc.Validate()
  • Retrieving instances with gioc.Get[T]()

Code explanation

The example creates a three-layer dependency chain using the global default container:

  1. UserRepository – data access layer (no dependencies)
  2. UserService – depends on *UserRepository
  3. UserHandler – depends on *UserService

All constructors are registered up front. Validate() checks for missing dependencies and cycles at startup. When Get[*UserHandler]() is called, the container resolves the full dependency chain automatically, creating each component exactly once.

Output

NewUserRepository called
NewUserService called
NewUserHandler called
handler1 == handler2: true (singleton)

Each constructor runs once. Both handler1 and handler2 point to the same instance.

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