1

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, "[email protected]"}}
b := B{&X{"Greg", 22, "[email protected]"}}
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?

1 Answer 1

2

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).

Sign up to request clarification or add additional context in comments.

2 Comments

Oh wow, that's handy! I'm guessing then this means the underlying structure of a struct with an embedded X is different to that of a struct if it had all the values of X explicitly declared. I ask because I'm using gorm which uses a struct to represent a table in the database
@GregPeckory Yes, they are different, but libraries may choose to handle them "equally". When you embed a type, the container struct will have a value of the embedded type explicitly. Fields and methods of the embedded type are promoted, but that is just syntactic sugar.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.