Sunday, February 8, 2026
Struct
Akash AmanUpdated: February 2026
Struct
Basics
Concept: A user-defined type that groups together related data of different types into a single unit. Think of it as a "schema" for your data.
- Why: Unlike arrays or slices which hold the same type, structs allow you to represent complex real-world entities (like a User, a Product, or a Configuration) by combining different data types.
- Behavior: Structs are value types. When you pass a struct to a function, Go creates a copy of the entire data structure unless you pass a pointer (
*).
Declaring and Initializing
Concept: Creating an instance of your defined struct.
- Behavior:
- Zero Value: If you declare a struct without values, every field is initialized to its "zero value" (0, "", false, nil).
- Named Fields: The recommended way to initialize, as it makes the code readable and resilient to schema changes.
- Fieldless (Anonymous): You can provide values without field names, but they must be in the exact order defined in the struct.
Accessing Fields
Concept: Reading or modifying the data stored within a struct instance.
- Behavior: Go uses the dot notation (
.). A unique feature of Go is that it provides automatic dereferencing. Even if you have a pointer to a struct, you use.instead of the->operator found in C/C++. - Why: This simplifies the syntax and makes code more consistent whether you are working with values or pointers.
Encapsulation & Exported Fields
Concept: Controlling which parts of your struct are visible to other packages.
-
Behavior:
- Upper Case: Field names starting with a Capital letter are Exported (Public).
- Lower Case: Field names starting with a lowercase letter are Unexported (Private to the package).
-
Why: This enforces data integrity. You can hide sensitive internal state (like a password hash) while exposing public identifiers.
Methods (Value vs. Pointer Receivers)
Concept: Functions attached to a specific type.
- Behavior:
- Value Receiver:
(u User). Operates on a copy. Changes inside the method won't affect the original. - Pointer Receiver:
(u *User). Operates on the original memory. Changes will persist.
- Value Receiver:
- Why: Use Value receivers for "read-only" operations to ensure safety. Use Pointer receivers when you need to update data or if the struct is very large (to avoid the cost of copying).
Anonymous Structs & Fields
Concept: Structs without a name, or fields without an explicit name.
-
Behavior:
- Anonymous Struct: Used for one-off data shapes (like JSON responses in tests).
- Without Field Name (Embedding): If you give a field a type but no name, the field name becomes the type name.
-
Why: This reduces boilerplate when you just need a temporary container for data.
Embedding and Composition
Concept: Building complex structs by "nesting" one struct inside another.
- Behavior: Go does not have classes or inheritance. Instead, it uses Struct Embedding. When you embed a struct without a field name, the outer struct "promotes" the fields of the inner struct.
- Why: It promotes the principle of Composition over Inheritance. It allows you to share behavior and data across types without the rigid hierarchy of traditional OOP.
Struct Tags
Concept: Metadata attached to struct fields that can be read at runtime using Reflection.
- Behavior: Usually written as backticked strings. They are most commonly used to tell encoders (like JSON or XML) how to handle the field.
- Why: It allows your data models to be "aware" of external formats without changing the core Go logic.
Reference Guides
Struct Properties Summary
| Property | Behavior | Why it matters |
|---|---|---|
| Value Semantics | Structs are copied on assignment. | Predictable memory behavior and thread safety. |
| Zero-Value Safe | Default values are always applied. | Prevents "undefined" or "null" pointer errors for fields. |
| Promotion | Embedded fields are accessed directly. | Simplifies code and reduces boilerplate code. |
| Fixed Memory | Size is known at compile time. | High performance and cache-friendly data layouts. |
Quick Cheat Sheet
| Task | Syntax Example |
|---|---|
| Define | type Name struct { ... } |
| Pointer Init | u := &User{} |
| Anonymous Struct | data := struct{ ID int }{ 1 } |
| JSON Mapping | json:"key_name" |
| Field Access | instance.FieldName |