Skip to content

Instantly share code, notes, and snippets.

@dlsniper
Created September 18, 2016 20:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dlsniper/7942b985b3bca1054e0d26641bd014d9 to your computer and use it in GitHub Desktop.
Save dlsniper/7942b985b3bca1054e0d26641bd014d9 to your computer and use it in GitHub Desktop.
Dependency injection in Go via wrapping the function
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")
}
@mohammad-vito
Copy link

Fantastic!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment