MCPcopy
hub / github.com/uber-go/fx / ExamplePopulate

Function ExamplePopulate

populate_example_test.go:30–55  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

28)
29
30func 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}

Callers

nothing calls this directly

Calls 5

ProvideFunction · 0.92
NewFunction · 0.92
PopulateFunction · 0.92
StartMethod · 0.45
StopMethod · 0.45

Tested by

no test coverage detected