Sunday, February 8, 2026

Struct

    Profile Pic of Akash AmanAkash Aman

    Updated: 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 (*).
    go
    // Defining a Struct
    type User struct {
        ID        int
        Username  string
        Email     string
        IsActive  bool
    }
    

    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.
    go
    // 1. Zero Value declaration
    var u User 
    
    // 2. Named field initialization (Recommended)
    admin := User{
        Username: "akash_aman",
        Email:    "akash@example.com",
        IsActive: true,
    }
    
    // 3. Without field names (Not recommended for large structs)
    guest := User{101, "guest_user", "guest@example.com", false}
    

    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.
    go
    p := &User{Username: "akash"}
    
    // No need for (*p).Username
    fmt.Println(p.Username) // Go handles the pointer dereference automatically
    

    Encapsulation & Exported Fields

    Concept: Controlling which parts of your struct are visible to other packages.

    bash
    myproject/
    ├── main.go
    └── models/
        └── user.go
    
    go
    // models/user.go
    type User struct {
        Name     string // Exported: Visible to main.go
        password string // Unexported: Hidden from main.go
    }
    
    • 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.
    • 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).
    go
    // Pointer Receiver: Changes the actual user
    func (u *User) Birthday() {
        u.Age++
    }
    
    // Value Receiver: Just returns a string
    func (u User) FullName() string {
        return u.FirstName + " " + u.LastName
    }
    

    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.

    go
    // Anonymous struct
    config := struct {
        APIKey string
    }{
        APIKey: "SECRET_123",
    }
    

    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.
    go
    type Permissions struct {
        CanEdit bool
    }
    
    type Admin struct {
        User        // Embedded (Anonymous field)
        Permissions // Embedded
        Level       int
    }
    
    // Usage:
    a := Admin{}
    a.CanEdit = true // Field promotion: you don't need a.Permissions.CanEdit
    

    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.
    go
    type User struct {
        // Tells the JSON library to use "user_id" instead of "ID"
        ID    int    `json:"user_id"` 
        Email string `json:"email,omitempty"` // Hide if empty
    }
    

    Reference Guides

    Struct Properties Summary

    PropertyBehaviorWhy it matters
    Value SemanticsStructs are copied on assignment.Predictable memory behavior and thread safety.
    Zero-Value SafeDefault values are always applied.Prevents "undefined" or "null" pointer errors for fields.
    PromotionEmbedded fields are accessed directly.Simplifies code and reduces boilerplate code.
    Fixed MemorySize is known at compile time.High performance and cache-friendly data layouts.

    Quick Cheat Sheet

    TaskSyntax Example
    Definetype Name struct { ... }
    Pointer Initu := &User{}
    Anonymous Structdata := struct{ ID int }{ 1 }
    JSON Mappingjson:"key_name"
    Field Accessinstance.FieldName

    © 2026 Akash Aman | All rights reserved