Mahdi.Kh
August 18, 2026
In this section, we will introduce `danceable/container` and `danceable/provider` for dependency management and service lifecycle management.
In the previous sections, we became familiar with DI, Service Containers, and Service Providers. In this section, we will explore the danceable/container and danceable/provider packages.
danceable/container as a service container in GoIn the previous section, we learned about the Service Container. In this section, we will explore danceable/container and learn how to use it. First, install the package by running the go get command as follows:
To create a container, use the New method:
With the Bind method we tell the container how to create a service:
With the Resolve method we ask the container to hand us a MongoDB connection:
In the example above, we first used the Resolve method to retrieve an instance of *mongo.Client, and then called the Ping method to ping the database.
As shown in the example, whenever we call the Resolve method, the function that we registered with the Container during is invoked. This function creates a value of type , which is then assigned to the variable we provided. If the last value returned by the function associated with is an error, that error is returned by the method. We can then check the error to make sure that the operation was successful.
Bind*mongo.ClientBindResolveResolveSometimes we need only one instance of a service to exist. In that case we can pass bind.Singleton() to the Bind method:
In the example above, we used bind.Singleton(). This means that even if we call Resolve multiple times, only one instance of *mongo.Client will be created and shared.
Note 1: As soon as we use the Bind method, the function passed to Bind is executed once so that any errors can be detected during the Bind operation. By using bind.Lazy(), we can tell the Container to delay the execution of the function passed to Bind until the first Resolve.
Note 2: When using bind.Singleton(), the Container caches the result of the first execution of the function passed to Bind and returns the same result on every subsequent Resolve.
The Call method asks the container to supply a function's parameters and invoke it:
The Fill method can also populate a struct's fields through the container. See the danceable/container documentation for more.
danceable/provider as a service provider in GoIn the previous part we covered the service provider concept. Here we get to know danceable/provider and learn how to use it.
Installing the package:
This package lets you define providers that take on binding your services and managing their lifecycle.
Every provider has at least these three methods:
Register: normally used to bind dependencies into the container.
Boot: runs after Register, normally used for initial startup work or for binding values that depend on the Register stage.
Terminate: runs before the program ends, normally used to release and close resources.
Once the providers exist, register them and then run them with the Run method:
When the Run method is called, the following steps take place:
First, the Register method of all Providers is executed in the order in which they were registered.
Then, the Boot method of all Providers is executed in the order in which they were registered.
At the end of the application, the Terminate method of all Providers is executed in the reverse order of their registration.
Note: According to the documentation, you can change the order of Providers by defining the Order method.
For more information about danceable/provider, refer to its documentation.