Skip to content

Instantly share code, notes, and snippets.

@jay-babu
Last active July 17, 2022 02:05
Show Gist options
  • Save jay-babu/f9d14ba3ce9bf1414b987dbcda45029f to your computer and use it in GitHub Desktop.
Save jay-babu/f9d14ba3ce9bf1414b987dbcda45029f to your computer and use it in GitHub Desktop.
Field Vs. Constructor Injection
package main
import (
"net/http"
)
type FieldInjection struct {
foo http.Server
bar http.Server
}
func NewFieldInjection() FieldInjection {
return FieldInjection{
foo: http.Server{},
bar: http.Server{},
}
}
// client using above struct
func client() {
// client can't pass in their own server
_ = NewFieldInjection()
}
type ConstructorInjection struct {
Foo http.Server
Bar http.Server
}
// client using above struct
func client() {
// client can determine the dependencies.
// ConstructorInjection is unopinionated.
_ = ConstructorInjection{
Foo: http.Server{},
Bar: http.Server{},
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment