Last active
October 14, 2017 13:25
-
-
Save donutloop/9bbbe68c671052880ce61dfe6b0bf2ea to your computer and use it in GitHub Desktop.
Using dependency injection in Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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