Go (Golang) / Object-Oriented Programming in Go
Working with Embedded Structs
This tutorial is about embedded structs in Go. We will explore how you can leverage embedding and composition to structure your Go code effectively.
Section overview
5 resourcesExplores how Go implements object-oriented concepts using structs and interfaces.
Working with Embedded Structs in Go
1. Introduction
This tutorial aims to help you understand how to work with embedded structs in Go. By the end of this tutorial, you should be able to:
- Understand what embedded structs are in Go
- Understand how to use and create embedded structs
- Leverage embedded structs for code organization and composition in Go
Prerequisites:
You should have a basic understanding of Go programming, including how to define and use structs.
2. Step-by-Step Guide
In Go, structs can contain other structs, and this concept is known as embedding. The idea behind this is instead of having a struct field with a name and type, you just specify the type. The struct you embed becomes a field of the struct it's included in, but without an explicit field name.
Best Practices:
- Use embedding for 'is-a' relationship, not 'has-a' relationship.
- Avoid embedding if it complicates your code. It should simplify and make your code more organized.
3. Code Examples
Example 1: Basic Example of Embedded Structs
package main
import "fmt"
// Defining a struct type
type Animal struct {
Name string
Age int
}
// Another struct type with Animal as embedded field
type Dog struct {
Breed string
Animal
}
func main() {
// Initializing Dog struct
d := Dog{
Breed: "Labrador",
Animal: Animal{
Name: "Max",
Age: 5,
},
}
fmt.Println(d)
}
In this example, Dog struct embeds the Animal struct. So, the Animal struct becomes a field in the Dog struct. The output of this program will be:
{Labrador {Max 5}}
Example 2: Accessing Fields of Embedded Structs
package main
import "fmt"
// Defining a struct type
type Animal struct {
Name string
Age int
}
// Another struct type with Animal as embedded field
type Dog struct {
Breed string
Animal
}
func main() {
// Initializing Dog struct
d := Dog{
Breed: "Labrador",
Animal: Animal{
Name: "Max",
Age: 5,
},
}
fmt.Println(d.Name)
fmt.Println(d.Age)
fmt.Println(d.Breed)
}
In this example, we access fields of the Animal struct directly from a Dog struct instance. The output will be:
Max
5
Labrador
4. Summary
In this tutorial, we have learned about embedded structs in Go. We've seen how to define them, how to create instances of structs with embedded structs, and how to access fields of embedded structs directly. Next, you can explore how to use embedded structs with interfaces for polymorphism.
Additional Resources:
- Go Documentation
- Effective Go
5. Practice Exercises
Exercise 1: Define a Circle struct that embeds a Point struct (with X and Y fields) and also has a Radius field. Create an instance of Circle, initialize it, and print its data.
Exercise 2: Extend the previous exercise by adding a method Area() to the Circle struct that calculates and returns the area of the circle. Print the area of the circle you created.
Solutions and explanations will be provided upon request.
Tips for Further Practice: Try to implement more complex programs using embedded structs. You can also explore the use of embedded interfaces in Go.
Need Help Implementing This?
We build custom systems, plugins, and scalable infrastructure.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI in Public Safety: Predictive Policing and Crime Prevention
In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…
Read articleAI in Mental Health: Assisting with Therapy and Diagnostics
In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…
Read articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article