supwr / uber-fx-demo

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Uber Fx by example

Testing several ways to use dependency injection with fx by creating a small app that consumes a simple API that returns a random user's data and prints it at the console.

There is a folder for each approach of using dependency injection with uber fx:

v1

The Golang's vanilla way of injecting the dependencies, by using func init() in the main.go file to instantiate the needed object and then passing it to the struct.

To run this version:

$ cd v1
$ go build -o v1
$ ./v1

v2

The way that i find the simplest of using uber-fx. Using fx.Provide, we can provide the constructor that instantiates the struct we need to inject.

To run this version:

$ cd v2
$ go build -o v2
$ ./v2

v3

Still using fx.Provide, but this time adding the use of fx.In and param objects. More details here

To run this version:

$ cd v3
$ go build -o v3
$ ./v3

v4

Mapping a struct to an interface, using fx.Annotate, as seen here

To run this version:

$ cd v4
$ go build -o v4
$ ./v4

v5

Using result objects to define what you intend to inject using fx.Out.

To run this version:

$ cd v4
$ go build -o v4
$ ./v4

Those are just some of the ways i found of using Uber Fx to make my life simple. But i surely will keep finding new ways and intend to keep this repo updated with all the new stuff i stumble upon.

About


Languages

Language:Go 100.0%