I have the following piece of code working correctly:
type Type struct {
p string
}
func foo() {
t := Type { p: "hola" }
bar(&t)
}
var bar = func(t *Type) {
t.p = "chau"
}
func Test1() {
var baz *Type
bar = func(t *Type) {
baz = t
}
foo()
fmt.Println(baz.p)
}
Basically I'm mocking the bar function in order to know the parameter that the function foo is passing when it runs. Now, suppose that I want to extract the logic of the mocking function in another function, how should I do it? I tried the following but got an invalid memory address or nil pointer dereference:
func mockedBar(b** Type) func(*Type) {
return func(t *Type) {
*b = t
}
}
func Test2() {
var baz **Type
bar = mockedBar(baz)
foo()
fmt.Println((**baz).p)
}