Last active
November 10, 2022 02:55
-
-
Save JoshCheek/fe5a03de0f0bf4336f4ad76110bdda22 to your computer and use it in GitHub Desktop.
whydoesntgowork.go
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 ("fmt") | |
// Real A (I cannot change this) | |
type RealA struct{} | |
func NewRealA() *RealA { return &RealA{} } | |
func (a *RealA) M() string { return "real a" } | |
// Fake A (I can change this) | |
type FakeA struct{} | |
func NewFakeA() *FakeA { return &FakeA{} } | |
func (a *FakeA) M() string { return "fake a" } | |
// interface for either Real or Fake A (I can change this) | |
type returnValue interface{ M() string } | |
type someFunc = func() returnValue | |
// fn that takes conceptually the Real A, but in tests, the Fake A (I can change this) | |
func greet(constructIt someFunc) string { return constructIt().M() } | |
// call it (I can change this) | |
func main() { | |
fmt.Println(greet(NewRealA)) | |
fmt.Println(greet(NewFakeA)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment