Created
September 18, 2016 20:44
-
-
Save dlsniper/bd6e3283190e3a76d92a260c4057aa7c to your computer and use it in GitHub Desktop.
Dependency injection in Go via explicit type
This file contains 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") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment