Skip to content

Instantly share code, notes, and snippets.

@donutloop
Last active October 14, 2017 13:25
Show Gist options
  • Save donutloop/9bbbe68c671052880ce61dfe6b0bf2ea to your computer and use it in GitHub Desktop.
Save donutloop/9bbbe68c671052880ce61dfe6b0bf2ea to your computer and use it in GitHub Desktop.
Using dependency injection in Go
package main
import "log"
type printerFunc func(message string, args ...interface{})
func helloWrapper(p printerFunc) func(string, ...interface{}) {
return func(message string, arguments ...interface{}) {
p(message, arguments...)
}
}
func main() {
hello := helloWrapper(log.Printf)
hello("hello %s", "world")
}
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")
}
package main
import "log"
type printerFunc func(message string, args ...interface{})
func hello(p printerFunc) {
p("hello %s", "world")
}
func main() {
hello(log.Printf)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment