
Composition allows you to create a new struct that embeds another struct. This can be useful for creating a new type that has some of the same fields as the original struct.
By using composition, you can create a new struct that has a reference to the original struct. This is demonstrated in the example where we create a new struct called "Address" that embeds the original "Person" struct.
Embedding is a more lightweight way to extend a struct, where one struct is embedded directly into another. This can be useful when you want to add some additional fields to an existing struct without creating a new type.
Suggestion: Golang Array of Structs
Composition in Go
In Go, you can create a struct by embedding one struct type into another, a concept known as composition.
Go promotes the use of composition over inheritance, allowing you to build complex types by combining simple ones, as seen in the example of a blog post composed of an author and content.
Here's an interesting read: Go High Level Twilio Integration
A struct in Go is a composite data type that groups variables under a single name, with fields of different types, such as Name and Age in a Person struct.
Composition can be achieved by embedding one struct type into another, as demonstrated by embedding the author struct into the blogPost struct.
The embedded fields can be accessed as if they were part of the outer struct, making the code more concise and efficient, as shown in the example where p.author.fullName() is replaced with p.fullName().
A different take: Check Type of Interface Golang
Structs and Embedding
In Go, you can create complex types by combining simple ones using a technique called embedding. This is done by placing one struct type inside another.
Embedding allows you to inherit fields and methods from the embedded struct, making them accessible through the outer struct. For example, if you embed an `author` struct within a `blogPost` struct, you can access the `author` fields directly from the `blogPost` instance.
The `author` struct can be defined with fields like `firstName`, `lastName`, and `bio`, and methods like `fullName()`. Then, the `blogPost` struct can embed the `author` struct and access its fields and methods.
You can also embed a struct within another struct using a simple syntax, like `struct { ... }`. This allows you to create complex types that can be easily composed and extended.
Note that not all types can be embedded. According to the Go specification, a type name `T` can be embedded unless `T` denotes a named pointer type or a pointer type whose base type is either a pointer or an interface type.
Here are the types that can be embedded:
- A type name `T` can be embedded unless `T` denotes a named pointer type or a pointer type whose base type is either a pointer or an interface type.
- A pointer type `*T`, where `T` is a type name denoting the base type of the pointer type, can be embedded unless type name `T` denotes a pointer or interface type.
By using composition instead of inheritance, you can create more flexible and maintainable code. This is a key concept in Go programming, and it's essential to understand how to use embedding to create complex types.
You might enjoy: Golang Create Error
Methods
You can invoke methods on embedded structs as if they were part of the outer struct. This is demonstrated by calling the Describe method on instances of Container.
Methods defined on an embedded struct can be called on the outer struct, even if the embedded struct is not explicitly mentioned. This is shown in the example where the FullAddress method can be called on Person because of embedding.
In Go, methods on embedded fields behave differently from inheritance in other languages. When a method is called on an embedded field, it's passed a receiver of the type of the embedded field, regardless of which embedding struct it's called through. This is a key difference between embedding in Go and classical inheritance.
Key Concepts
In Go, struct composition is used instead of class-based inheritance to acquire properties and behaviors from an existing class.
This approach promotes fields and methods to the outer struct, making it a key feature of Go's composition.
Go's use of interfaces enables polymorphism without requiring explicit inheritance.
Here are the key concepts to keep in mind when working with struct composition in Go:
- Struct composition instead of class-based inheritance
- Embedded structs promote fields and methods to the outer struct
- Interfaces enable polymorphism without requiring explicit inheritance
Featured Images: pexels.com


