03_prototypes

command
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

README

Flora Example: Prototypes (Factories)

This example demonstrates how to change the lifecycle of a component from Singleton (default) to Prototype (fresh instance per call).

The Concept

By default, Flora creates exactly one instance of each component per container. But sometimes you need a fresh instance, like a unique Session for every HTTP request or a temporary file handler.

If you add the struct tag flora:"scope=prototype" to your component, Flora changes its behavior. Instead of injecting the finished struct (e.g., *Session) into your consumers, Flora generates and injects a Factory Closure (func() *Session).

Calling this factory function creates a brand new instance on the fly and automatically resolves all of its deep dependencies!

How to run

  1. Generate the DI container (flora_container.go) in the current directory:
    flora gen -o .
    
    

2. Run the application:
```bash
go run .

Expected Output

-> [Init] Server initialized (Singleton)
-------------------------------------------------
-> [Factory] Created a brand new Session with ID: 4182
Server handling request for Alice using Session 4182

-> [Factory] Created a brand new Session with ID: 8917
Server handling request for Bob using Session 8917

(Note: The exact Session IDs will be randomly generated on your machine)

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