Skip to content

Instantly share code, notes, and snippets.

@p4tin
Last active July 9, 2018 01:41
Show Gist options
  • Save p4tin/7e0653aca3049b874d150b0e25387681 to your computer and use it in GitHub Desktop.
Save p4tin/7e0653aca3049b874d150b0e25387681 to your computer and use it in GitHub Desktop.
Simple dependancy Injection example
package main
import (
"fmt"
"testing"
)
var ScreenPrint = fmt.Printf
func Greet(name string) {
ScreenPrint("Hello, %s", name)
}
//func main() {
// Greet("Playground")
//}
func TestGreet(t *testing.T) {
var result string
ScreenPrint = func(format string, a ...interface{}) (n int, err error) {
result = fmt.Sprintf(format, a...)
n = len(result)
err = nil
return
}
expected := "Hello, Tester"
Greet("Tester")
if expected != result {
t.Errorf("got '%s' want '%s'", result, expected)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment