| 28 | ) |
| 29 | |
| 30 | func ExamplePopulate() { |
| 31 | // Some external module that provides a user name. |
| 32 | type Username string |
| 33 | UserModule := fx.Provide(func() Username { return "john" }) |
| 34 | |
| 35 | // We want to use Fx to wire up our constructors, but don't actually want to |
| 36 | // run the application - we just want to yank out the user name. |
| 37 | // |
| 38 | // This is common in unit tests, and is even easier with the fxtest |
| 39 | // package's RequireStart and RequireStop helpers. |
| 40 | var user Username |
| 41 | app := fx.New( |
| 42 | UserModule, |
| 43 | fx.NopLogger, // silence test output |
| 44 | fx.Populate(&user), |
| 45 | ) |
| 46 | if err := app.Start(context.Background()); err != nil { |
| 47 | panic(err) |
| 48 | } |
| 49 | defer app.Stop(context.Background()) |
| 50 | |
| 51 | fmt.Println(user) |
| 52 | |
| 53 | // Output: |
| 54 | // john |
| 55 | } |