Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Last active November 10, 2022 02:55
Show Gist options
  • Save JoshCheek/fe5a03de0f0bf4336f4ad76110bdda22 to your computer and use it in GitHub Desktop.
Save JoshCheek/fe5a03de0f0bf4336f4ad76110bdda22 to your computer and use it in GitHub Desktop.
whydoesntgowork.go
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