Join us

Type embedding: Golang's fake inheritance

Type embedding: Golang's fake inheritance

Golang allows for type embedding, which is a way to embed a type inside another type using a struct. It gives a syntactic shortcut for accessing all of the embedded type's fields and allows you to share methods on the embedded type.

However, it's important to note that type embedding isn't inheritance, and pretending it is will lead to bugs. Common use cases where the analogy between type embedding and inheritance breaks down include special keywords, such as "this" in Java, method chaining, and interface embedding.

Despite its flaws, type embedding is still widely used, but it's crucial to be aware of Golang's limitations and write code defensively with them in mind.


Let's keep in touch!

Stay updated with my latest posts and news. I share insights, updates, and exclusive content.

Unsubscribe anytime. By subscribing, you share your email with @faun and accept our Terms & Privacy.

Give a Pawfive to this post!


Only registered users can post comments. Please, login or signup.

Start writing about what excites you in tech — connect with developers, grow your voice, and get rewarded.

Join other developers and claim your FAUN.dev() account now!

Avatar

The FAUN

FAUN.dev()

@faun
The FAUN watches over the forest of developers. It roams between Kubernetes clusters, code caves, AI trails, and cloud canopies, gathering the signals that matter and clearing out the noise.
Developer Influence
3k

Influence

302k

Total Hits

3711

Posts