Hi everyone,
I’ve been exploring how dependency injection (DI) works in ASP NET Core and am curious about its equivalent in Go. Here’s a quick explanation of what I understand so far in ASP NET Core:
Using AddSingleton in ASP NET Core, we can register a service as a singleton, meaning the framework creates a single instance of the service and shares it throughout the application lifetime. For example, we typically add services in the Program.cs or Startup.cs file like this:
builder.Services.AddSingleton<IMyService, MyService>();
Then, wherever IMyService is needed (e.g., in controllers or other services), ASP NET Core automatically injects the single shared instance.
My Questions:
What is the Go equivalent of this approach, particularly for managing singleton classes and dependency injection?
Is the AddSingleton-style approach (used in ASP NET Core) good for Go, or are there better, more idiomatic ways to manage dependencies and singletons in Go?
I’d love to hear insights or examples on how dependency injection is typically handled in Go, especially when compared to frameworks like ASP NET Core.
Thanks in advance!