Skip to main content

Upgrade samber/do from v1.x to v2

This documentation will help you upgrade your application from samber/do v1 to samber/do v2.

samber/do v2 is a new major version, including breaking changes requiring you to adjust your applications accordingly. We will guide to during this process and also mention a few optional recommendations.

This release is a large rewrite, but the breaking changes are relatively easy to handle. Some updates can be done with a simple sed command.

Check the release notes here.

No breaking change will be done until v3.

1- Upgrading package

Update go.mod:

go get -u github.com/samber/do/v2

Replace package import:

find . -name '*.go' -type f -exec sed -i '' 's#samber/do"$#samber/do/v2"#g' {} \;

Cleanup previous dependencies:

go mod tidy

2- do.Injector interface

do.Injector has been transformed into an interface. Replace *do.Injector by do.Injector.

find . -name '*.go' -type f -exec sed -i '' "s/*do.Injector/do.Injector/g" {} \;

3- Shutdown

do.ShutdownOnSignals used to return only 1 argument.

# from
err := injector.ShutdownOnSignals(syscall.SIGTERM, os.Interrupt)

# to
signal, err := injector.ShutdownOnSignals(syscall.SIGTERM, os.Interrupt)

injector.ShutdownOnSIGTERM() has been removed. Use injector.ShutdownOnSignals(syscall.SIGTERM) instead.

injector.Shutdown() now returns a map of errors (map[string]error) and is non-blocking in case of failure of a single service.

4- Internal service naming

Internally, the DI container stores a service by its name (string) that represents its type. In do@v1, some developers reported collisions in service names, because the package name was not included.

Eg: *mypkg.MyService -> *github.com/samber/example.MyService.

In case you invoke a service by its name (highly discouraged), you should make some changes.

To scan a project at the speed light, just run:

grep -nrE 'InvokeNamed|OverrideNamed|HealthCheckNamed|ShutdownNamed' .