Structs in Go
# CHAPTER 14
Structs in Go
1. Introduction
If you are building an Employee Management System, you can't manage an employee using 5 separate variables (name, age, salary, email, id). You need to group them into a single, logical unit.
In Java or Python, you would use a Class. Go does NOT have classes. Instead, Go uses Structs. Structs form the entire foundation of Object-Oriented Programming (OOP) in Go.
2. Learning Objectives
By the end of this chapter, you will be able to:- Define a custom Struct type.
- Instantiate and initialize Structs.
- Access and modify Struct fields.
- Pass Structs using pointers.
- Understand Composition using Embedded Structs.
3. Defining and Using a Struct
A struct is a typed collection of fields.4. Zero Values in Structs
If you instantiate a struct without providing all the fields, Go automatically fills the missing fields with their Zero Values.5. Passing Structs (Pointers are Critical!)
If anEmployee struct has 50 fields, passing it to a function UpdateSalary(emp Employee) will make a copy of all 50 fields in memory. This is incredibly slow.
You should almost always pass Structs using Pointers to modify the original and save memory.
6. Embedded Structs (Composition over Inheritance)
Because Go does not have Classes, it does not have Inheritance (class Manager extends Employee).
Instead, Go uses Composition. You can embed one struct inside another.
7. Anonymous Structs
If you only need a struct for a single, one-off use (very common when parsing JSON responses), you can create an Anonymous Struct without formally defining atype.
8. Common Mistakes
-
Trailing Commas: When initializing a multi-line struct, Go strictly requires a comma
,after the very last field. If you omit it, the code will not compile.
- Assuming Inheritance: Trying to cast an embedded struct to its parent struct. Go is not an OOP language in the traditional sense; do not try to force Java-style hierarchies.
9. Best Practices
-
Always use Named Fields during initialization (
Employee{Name: "Bob"}). It prevents bugs if you later add new fields to the struct definition.
10. Exercises
-
1.
Define a
Carstruct with fieldsMake,Model, andYear.
-
2.
Instantiate a
Carobject and print itsMake.
-
3.
Create an
UpdateYearfunction that takes a pointer to aCarand changes the year. Test it.
11. MCQs with Answers & Explanations
Q1. Does Go have traditional Classes? a) Yes b) No Answer: b) No. *Explanation: Go uses Structs to group data.*
What keyword is used to define a custom grouping of fields in Go?
How do you access a specific field inside an instantiated struct?
What happens if you initialize a struct but omit one of the fields?
Why is it a best practice to pass structs to functions using pointers?
(*emp).Name) to access its fields?
a) Yes b) No, Go automatically dereferences it so you can just use emp.Name
Answer: b) No, Go automatically dereferences it.
Since Go lacks Inheritance, how do you reuse struct fields in another struct?
What is an Anonymous Struct?
When initializing a multi-line struct, what syntax quirk does Go strictly enforce?
12. Interview Preparation
Interview Questions:- 1. Go does not have Classes or Inheritance. How does it achieve Object-Oriented modeling?
- 2. Explain Composition over Inheritance and provide a brief code example.
13. Summary
Structs are Go's equivalent to Classes. They allow you to group multiple data types into a single, cohesive entity. By utilizing embedded structs (Composition) and passing them via pointers, you can design highly performant, scalable data models without the bloated hierarchies of traditional OOP.14. Next Chapter Recommendation
Structs currently only hold *data*. But what if anEmployee struct needs an action, like a CalculateTax() method? In Chapter 15: Methods and Interfaces, we will learn how to attach functions directly to structs and explore Go's incredibly powerful Interface system.