Search code examples
pointersgostructembedding

Assigning embedded Struct type values


Suppose I have the following struct:

type X struct{
    Name string
    Age int
    Email string
}

and then I have 2 other structs (subclasses):

type A struct{
    *X
}

type B struct{
    *X
}

Then in my main code I have:

a := A{&X{"John", 34, "jd@email.com"}}
b := B{&X{"Greg", 22, "gd@email.com"}}
fmt.Println(a.Name) // John
fmt.Println(b.Name) //Greg

Is there a simple way to assign a=b such that a.Name returns Greg as well as all the other properties (I don't want to assign them individually because there's a bunch of properties)

Note: I have to do it this way as I have two identical database tables (in terms of their properties) and the ORM I'm using bases the target table on the struct name. Speaking of, I'm not sure how the ORM identifies the "structure" of the struct to perform this logic, but does using embedded fields, instead of standard fields affect this?


Solution

  • The unqualified type name acts as the field name when embedding, so you may refer to the embedded value as a.X and b.X.

    You may also assign one to the other:

    a.X = b.X
    fmt.Println(a.Name) // Greg
    fmt.Println(b.Name) // Greg
    

    After this, both will print Greg. Try it on the Go Playground.

    One thing to note here. Since your structs embed a pointer, the above assignment a.X = b.X assigns the pointer value. So after the above modifying fields of one will affect the other.

    So continuing the above example:

    a.X.Name = "Bob"
    fmt.Println(a.Name) // Bob
    fmt.Println(b.Name) // Bob
    

    Both names changed to Bob (try it). If you don't want that, then assign the pointed values, not the pointers:

    *a.X = *b.X
    fmt.Println(a.Name) // Greg
    fmt.Println(b.Name) // Greg
    

    This will again print Greg twice. Try it on the Go Playground.

    And now if you do this:

    a.X.Name = "Bob"
    fmt.Println(a.Name) // Bob
    fmt.Println(b.Name) // Greg
    

    Only a.Name changed, but not b.Name (try it).