Struct with an embedded method to another struct

Imagine I have the following struct:

type S1 struct {
	Field1 bool
	Field2 int
}

I can create another struct that embeds it and some a function Foo():

type S2 struct {
	S1
	Foo func()
}

This is fine. However, I’d like to make Foo() a method of S1, but I have no idea how to include it in S2. I can easily make it a function calling S1:

type S2 struct {
	S1
	Foo func(S1)
}

but a method?

Do I need to use an interface for that, or is there a simple way of doing so without calling an interface?

you can make Foo() a method of S1:

type S1 struct {
    field1 bool
    field2 int
}

func (s *S1) Foo() {...}

and then call it from S2
S2.Foo() or S2.S1.Foo()

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.

You can’t call a function in a field as if it were a bound function, but you can put a helper function that’ll do it:

type BigStruct struct {
  DoSomethingValue
  DoSomethingFunc func(*BigStruct)
}

func (b *BigStruct) DoSomething() {
  b.DoSomethingValue = b.DoSomethingFunc(b)
}
1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.