Go – Structs, Interfaces & Composition
Estimated reading: 3 minutes 55 views

🧱 Go – Structs Explained with Syntax, Examples, and Best Practices (2025 Guide)

🧲 Introduction – What Are Structs in Go?

A struct in Go is a composite data type that groups together variables (called fields) under a single name. Structs are used to model real-world entities, custom data types, and record-like objects such as users, configurations, or responses.

🎯 In this section, you’ll learn:

  • How to define and initialize structs
  • Access and modify struct fields
  • Use struct literals and pointers
  • Best practices and struct comparisons

✅ Define a Struct

type Person struct {
    Name string
    Age  int
}

✅ This defines a new Person type with two fields: Name and Age.


🧩 Initialize a Struct

🔹 Using Field Names

p1 := Person{Name: "Alice", Age: 30}

🔹 Positional (without field names)

p2 := Person{"Bob", 25}

⚠️ Positional syntax depends on field order – not recommended for large structs.


🧪 Access and Modify Struct Fields

fmt.Println(p1.Name) // Output: Alice

p1.Age = 31
fmt.Println(p1.Age)  // Output: 31

✅ Use dot notation to access or modify fields.


📦 Struct with Pointer

p := &Person{Name: "Charlie", Age: 40}
p.Age = 41  // Automatically dereferenced
fmt.Println(p.Name, p.Age)

📤 Output:

Charlie 41

✅ Go allows implicit dereferencing of struct pointers.


🔁 Struct Inside Struct (Nesting)

type Address struct {
    City  string
    State string
}

type Employee struct {
    Name    string
    Age     int
    Address Address
}

e := Employee{
    Name: "John",
    Age:  28,
    Address: Address{
        City:  "Pune",
        State: "MH",
    },
}

✅ Nesting allows hierarchical data modeling.


✨ Anonymous Fields (Embedded Structs)

type Contact struct {
    Phone string
}

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

u := User{Name: "Eva", Contact: Contact{Phone: "123456"}}
fmt.Println(u.Phone) // Access without dot chain

✅ Embedding promotes fields to outer struct – simulates inheritance.


📚 Compare Structs

p1 := Person{"Tom", 20}
p2 := Person{"Tom", 20}
fmt.Println(p1 == p2) // Output: true

✅ Go supports direct struct comparison if all fields are comparable.


🧠 Best Practices

TipWhy It Matters
✅ Use named fieldsImproves readability and maintainability
✅ Use pointers for large structsAvoids copying large memory blocks
❌ Don’t use positional initRisky with field order changes
✅ Use embedded structsFor composition and reusability

📌 Summary – Recap & Next Steps

Go structs are the foundation of custom types in Go. They provide a way to encapsulate related fields and represent real-world models in a clean and efficient manner.

🔍 Key Takeaways:

  • Structs group fields of different types
  • Use dot notation to access fields
  • Use pointers to avoid copying and enable mutation
  • Support for nesting and embedding allows advanced data models

⚙️ Next: Explore Methods on Structs, Struct Tags, or JSON Struct Encoding.


❓ FAQs – Go Structs

❓ Can I compare two structs directly in Go?
✅ Yes, if all fields are comparable types.

❓ What is the zero value of a struct?
✅ All fields are initialized to their respective zero values.

❓ How do I create a pointer to a struct?
✅ Use &StructType{} or new(StructType).

❓ Can I embed one struct into another?
✅ Yes. It allows field promotion and composition.

❓ Should I pass structs by value or by pointer?
✅ Use pointers for large structs or when mutation is needed.


Share Now :
Share

Go – Structs

Or Copy Link

CONTENTS
Scroll to Top