Skip to content

Instantly share code, notes, and snippets.

@maxp-edcast
Last active October 29, 2018 23:12
Show Gist options
  • Save maxp-edcast/e50d96350fec0bdcef1a57dab7832449 to your computer and use it in GitHub Desktop.
Save maxp-edcast/e50d96350fec0bdcef1a57dab7832449 to your computer and use it in GitHub Desktop.
mocked instance method in golang
package main
import "fmt"
type myType struct {
fn func(int) int
}
func main () {
inst := myType{fn: func(i int) int { return i }}
origFn := inst.fn
var passedArg int
inst.fn = func(i int) int {
passedArg = i
return origFn(i + 1)
}
res := inst.fn(1)
fmt.Println("fn called with arg %d returned %d", passedArg, res)
// => "fn called with arg 1 returned 2"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment