Last active
October 29, 2018 23:12
-
-
Save maxp-edcast/e50d96350fec0bdcef1a57dab7832449 to your computer and use it in GitHub Desktop.
mocked instance method in golang
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" | |
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