Thanks, I appreciate your reply. Unfortunately, this is not what I need in this particular situation. I need a pointer to the method in this struct (actually, since functions are passed by reference, no need to use a pointer). I am working on a general application, and I need to enable the user to define their own methods and point out in the struct which one is to be used. Hence it’s not enough for me to just define a method. Had it been a function, no problem; but I don’t know how to do it with methods.
(It is possible that there are other approaches — still a Go newbie, and I am trying to learn code design in Go, which sometimes can be so different from that in other languages.)
Can you write some pseudocode that demonstrates how you’d like to use the function field? I’m not sure what you’re going for, but you could do stuff like this: https://play.golang.org/p/3lbc8dIEFkS
Thanks Sean. Point is not in what I want to achieve, but how want to do it. Let me explain.
I am creating a package that is based upon a big struct that embeds a number of other (also rather big) structs. There are plenty of methods involved, all being based upon this one big struct. One of the elements of this struct is a function, and this function also uses this one big struct. This function can be changed by the user (I mean, the user can use another function), so in fact I am just pointing to this function from the struct (but since functions are passed by reference, no need to directly use a pointer). Let’s assume we have a BigStruct struct, but I will mock it with something small:
type BigStruct struct {
Whatever int
BigStructFuncValue
BigStructFunc func(BigStruct)
}
This will work indeed. And this is fine. However, BigStructFunc here will lead to the change of BigStructFuncValue. To achieve this, I will need to do the following:
var bs BigStruct
bs.BigStructFuncValue = BigStructFunc(bs)
My question is whether I can in a similar simple way replace BigStructFunc(BigStruct) in the struct definition with the corresponding method. Then, I could do the following:
var bs BigStruct
bs.BigStructFunc()
and this would automatically affect bs.BigStructFuncValue. I want to do so since the whole package uses many methods of the BigStruct struct, and so, for consistency and elegance, I would prefer bs.BigStructFunc to be a method than a function.