When an object requires a dependency to work, the easiest way to ensure that dependency is always available is to require all users to supply it as a parameter to the object's constructor. This is known as constructor injection.
Let's work through an example where we will extract a dependency, generalize it, and achieve constructor injection. Say we are we are building a website for an online community. For this site, we wish to send an email to new users when they sign up. The code for this could be like this:
// WelcomeSender sends a Welcome email to new users
type WelcomeSender struct {
mailer *Mailer
}
func (w *WelcomeSender) Send(to string) error {
body := w.buildMessage()
return w.mailer.Send(to, body)
}
We've made the *Mailer private to ensure proper encapsulation of the internals of the class. We can inject the *Mailer dependency...