Skip to main content
Go Language Fundamentals for Beginners to Advanced
CHAPTER 14 Beginner

Structs in Go

Updated: May 17, 2026
5 min read

# 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.
go
1234567891011121314151617181920212223242526
package main
import "fmt"

// 1. Define the Struct (Usually outside the main function)
type Employee struct {
    ID     int
    Name   string
    Salary float64
    IsLead bool
}

func main() {
    // 2. Instantiate using named fields (Best Practice)
    emp1 := Employee{
        ID:     101,
        Name:   "Alice",
        Salary: 85000.00,
        IsLead: true, // Note the trailing comma!
    }

    // 3. Accessing fields using the dot (.) operator
    fmt.Println("Employee Name:", emp1.Name)
    
    // Modifying a field
    emp1.Salary = 90000.00
}

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.
go
123
emp2 := Employee{Name: "Bob"}
fmt.Println(emp2.ID) // Prints 0
fmt.Println(emp2.IsLead) // Prints false

5. Passing Structs (Pointers are Critical!)

If an Employee 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.
go
12345678910111213
// Accepts a pointer to an Employee
func GiveRaise(emp *Employee) {
    emp.Salary += 5000 
    // Notice we use emp.Salary, not (*emp).Salary. 
    // Go automatically dereferences struct pointers for you!
}

func main() {
    emp := Employee{Name: "Charlie", Salary: 50000}
    
    GiveRaise(&emp) // Pass the memory address
    fmt.Println(emp.Salary) // 55000
}

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.
go
12345678910111213141516171819202122
type Address struct {
    City  string
    State string
}

type User struct {
    Name    string
    Contact Address // Embedded Struct
}

func main() {
    u := User{
        Name: "David",
        Contact: Address{
            City:  "New York",
            State: "NY",
        },
    }

    // Access nested fields
    fmt.Println(u.Contact.City) // Prints "New York"
}

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 a type.
go
12345678
config := struct {
    Port int
    Env  string
}{
    Port: 8080,
    Env:  "Production",
}
fmt.Println(config.Port)

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. 1. Define a Car struct with fields Make, Model, and Year.
  1. 2. Instantiate a Car object and print its Make.
  1. 3. Create an UpdateYear function that takes a pointer to a Car and 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.*

Question 2

What keyword is used to define a custom grouping of fields in Go?

Question 3

How do you access a specific field inside an instantiated struct?

Question 4

What happens if you initialize a struct but omit one of the fields?

Question 5

Why is it a best practice to pass structs to functions using pointers?

Q6. Do you need to explicitly dereference a struct pointer (e.g., (*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.
Question 7

Since Go lacks Inheritance, how do you reuse struct fields in another struct?

Question 8

What is an Anonymous Struct?

Question 9

When initializing a multi-line struct, what syntax quirk does Go strictly enforce?

Q10. Can a struct contain a Slice as one of its fields? a) Yes b) No Answer: a) Yes.

12. Interview Preparation

Interview Questions:
  1. 1. Go does not have Classes or Inheritance. How does it achieve Object-Oriented modeling?
  1. 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 an Employee 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.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·