Skip to content

Instantly share code, notes, and snippets.

@dlsniper
Created September 18, 2016 20:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dlsniper/bd6e3283190e3a76d92a260c4057aa7c to your computer and use it in GitHub Desktop.
Save dlsniper/bd6e3283190e3a76d92a260c4057aa7c to your computer and use it in GitHub Desktop.
Dependency injection in Go via explicit type
package main
import "log"
type printerFunc func(message string, args ...interface{})
type demo struct {
printer printerFunc
}
func (d *demo) hello(message string, args ...interface{}) {
d.printer(message, args...)
}
func newDemo(printer printerFunc) *demo {
return &demo{
printer: printer,
}
}
func main() {
d := newDemo(log.Printf)
d.hello("hello %s", "world")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment