3

Consider the following type declaration:

type (
    Embedded struct{}
    Actual1 struct{ *Embedded }
    Actual2 struct{ *Embedded }
    Actual3 struct{ *Embedded }
)

Now consider the following function, where i may be of type Actual1, Actual2, or Actual3 (or any other type that embeds Embedded in like manner). I can't do a type assertion or a type switch because I can't know how many types contain Embedded, all I know about i is that it does indeed embed the Embedded type. This function will instantiate a new instance of the same type as i and set embed on that newly instantiated copy instance.

func New(i interface{}, field *Embedded) interface{} {
    // Step 1. instantiate new instance of `i`, of same underlying type as `i`
    // Step 2. set `i.Embedded` to `field`
    // Step 3. return the new instance.
}

Here's what usage would look like:

func main() {
    actual := &Actual1{}
    embed := &Embedded{}
    copied := New(actual, embed)
    if copied.(Actual1).Embedded != embed {
        log.Fatal("It didn't work!")
    }
}

A correct implementation of the New(...) function cannot use type assertions or a type switch and would also not result in the call to log.Fatal shown above.

I think what I'm asking for is a combination of these two questions:

Community
  • 1
  • 1
Michael Whatcott
  • 5,603
  • 6
  • 36
  • 50

1 Answers1

2

Using reflection, you can do it like that:

  • get the type of the struct from the type of the pointer to it
  • instantiate and deref it
  • set the value of the field (looking it up by name) with the value of your pointer

Code:

v := reflect.New(reflect.TypeOf(i).Elem()).Elem()
f := reflect.ValueOf(field)
v.FieldByName("Embedded").Set(f)
return v.Interface()

Playground: http://play.golang.org/p/fX413svXDv

Ainar-G
  • 34,563
  • 13
  • 93
  • 119