Golang's Neat (And Possibly Frustrating) Feature About Embedded Types

gopher_head-min

I recently learned about a cool (and possibly frustrating) feature of Go. I haven't found any information on this (I'm probably not searching for the right terms) so I'll call it type attribute shortcuts. Check out and run this code.

As you can see, Go helpfully creates a shortcut for types embedded in other types (in this example, Person.ContactInfo.Email can be accessed through Person.Email). But if the name of an attribute on the parent type conflicts with the child type, it takes precedence over the child type. The rest of the sub-type's attributes are still accessible though.

gopher_head-min