I am new to go-lang and I try to figure out how to work properly with structs and dependency injection. I am a bit stuck because I am not able to properly store a reference to another struct.
Here's my method that generates a CommandController. There is a valid reference to iris.Application.
func ProvideCommandController(application *iris.Application, commandRepository command.CommandRepository) (*interfaces.CommandController, error) {
commandController := interfaces.CommandController{}
commandController.Init(application, commandRepository)
commandController.Start()
return &commandController, nil
}
The struct looks like this:
type CommandController struct {
commandRepository command.CommandRepository
app *iris.Application
}
func (c CommandController) Init(app *iris.Application, repository command.CommandRepository) {
c.app = app
c.commandRepository = repository
}
func (c CommandController) Start() {
c.app.Get("/command", c.readAll)
c.app.Get("/command/{id:string}/execute", c.executeCommand)
c.app.Run(iris.Addr(":8080"))
}
When the ProvideCommandController
function gets executed I can debug and observe that all references look good. Unfortunately, commandController.Start()
fails because of c.app
is nil.
What piece of understanding do I miss? Somehow, the stored reference get's deleted between the Init and the Start function call.
Thanks in advance :)