Go (Golang) Tutorial 0/45 lessons ~6 min read Lesson 15

    Structs

    Structs group named fields into a single type — Go's primary data composition mechanism.

    Course progress0%
    Focus
    10 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    Structs group named fields into a single type — Go's primary data composition mechanism. Structs are value types: assignment copies all fields. Embedded structs (anonymous fields) enable composition over inheritance, a core Go design philosophy.

    Struct tags on fields drive JSON encoding, database column mapping, and validation. Understanding field visibility (exported vs unexported) and zero-value initialization builds the foundation for domain models in microservices.

    Interviewers ask struct vs class, embedding vs inheritance, and memory layout — this lesson covers all three with production examples.

    The story

    A Docker Compose metadata service models each container as a struct with image name, port mappings, and health-check interval. Structs group related fields with named types — when marshaled to JSON for a REST API, field tags control serialization without hand-writing parsers.

    Embedded structs compose configuration: a BaseService with logging fields gets promoted into PaymentService, mirroring how Kubernetes CRD specs embed standard metadata across hundreds of custom resources.

    Understanding the topic

    Key concepts

    • type Person struct { Name string; Age int } — value type.
    • Field visibility: uppercase exported, lowercase package-private.
    • Struct literals: Person{Name: "Alice", Age: 30} or positional Person{"Alice", 30}.
    • Embedding: type Admin struct { User; Level int } promotes User fields.
    • Struct tags: `json:"name" db:"user_name"` for marshaling/ORM.
    • Comparable structs if all fields comparable — usable as map keys.

    Step-by-step explanation

    1. Define type with struct keyword and field list.
    2. Create with literal or var zero initialization.
    3. Access fields with dot notation; pointer fields use automatic dereference.
    4. Embedded fields promote methods and fields to outer type.
    5. Pass struct by value (copy) or pointer (*Struct) for mutation.
    6. reflect or encoding/json reads tags at runtime.

    Practical code example

    Domain struct with embedding, tags, and constructor pattern:

    go
    package main
    import (
    "encoding/json"
    "fmt"
    "time"
    )
    type User struct {
    ID string `json:"id"`
    Email string `json:"email"`
    CreatedAt time.Time `json:"created_at"`
    }
    type Admin struct {
    User
    Role string `json:"role"`
    }
    func NewUser(id, email string, createdAt time.Time) User {
    return User{ID: id, Email: email, CreatedAt: createdAt.UTC()}
    }
    func main() {
    admin := Admin{
    User: NewUser("u1", "admin@techlearningpro.com", time.Date(2026, time.July, 1, 12, 0, 0, 0, time.UTC)),
    Role: "superadmin",
    }
    data, _ := json.Marshal(admin)
    fmt.Println(string(data))
    fmt.Println("email via promotion:", admin.Email)
    }

    Output

    {"id":"u1","email":"admin@techlearningpro.com","created_at":"2026-07-01T12:00:00Z","role":"superadmin"}
    email via promotion: admin@techlearningpro.com

    Line-by-line code explanation

    • type User struct { defines a struct with ID, Email and CreatedAt fields, the last one using the time.Time type.
    • `json:"created_at"` is a struct tag. It tells the encoding/json package to use the key created_at in JSON instead of the Go field name; the other fields have tags too.
    • type Admin struct { defines a second struct; its first line inside, with just User, embeds the User type with no field name, and it also has a Role field.
    • func NewUser(id, email string, createdAt time.Time) User { is a constructor function that builds and returns a User value.
    • CreatedAt: createdAt.UTC() sets the field using a keyed struct literal, converting the time to UTC first.
    • time.Date(2026, time.July, 1, 12, 0, 0, 0, time.UTC) builds a fixed time, July 1 2026 at 12:00 in UTC, which is passed to NewUser.
    • Role: "superadmin", fills the Admin's own Role field; the User field above it is set to the value returned by NewUser.
    • data, _ := json.Marshal(admin) converts the struct to JSON bytes. The error is ignored with _. Fields of the embedded User appear at the top level of the JSON next to role. The next line converts the bytes to a string and prints them.
    • admin.Email reads Email straight from the Admin, because the embedded User's fields are promoted. admin.User.Email would give the same value.

    Key takeaway: Prefer constructor functions (NewUser) for invariants. Embedding promotes fields — not inheritance. Tags must match serializer conventions.

    Real-world use

    Where you'll use this in production

    • Domain entities: User, Order, Product in microservices.
    • Configuration structs loaded from env or YAML.
    • API request/response DTOs with json tags.
    • Database models with db or gorm tags.

    Best practices

    • Use constructor functions to enforce valid initial state.
    • Prefer composition (embedding) over copying fields.
    • Keep structs focused — split large structs by responsibility.
    • Use pointer receivers when methods mutate state.
    • Align struct tags with API contract (snake_case JSON common).
    • Document exported struct fields in godoc.

    Common mistakes

    • Huge god structs with 20+ fields — split by domain.
    • Exporting all fields when validation needed — use constructor.
    • Copying large structs in hot paths — use pointers.
    • Wrong json tag names breaking API clients.
    • Embedding conflicting field names — inner wins, outer shadowed.

    Advanced interview questions

    Interview Prep

    Practice concise answers, then expand each card for the explanation.

    5 questions
    1BeginnerQuestionStruct vs class?+

    Answer

    Go has structs only — no classes; methods attached via receivers; composition over inheritance.
    2BeginnerQuestionValue vs pointer struct?+

    Answer

    Value copies on assign/pass; pointer shares single instance, enables mutation.
    3IntermediateQuestionEmbedding purpose?+

    Answer

    Composition — promotes embedded type's fields/methods to outer type without inheritance.
    4IntermediateQuestionStruct tags usage?+

    Answer

    Metadata for json, xml, db, validate — read by encoding/json and ORMs via reflection.
    5AdvancedQuestionDesign immutable Order with line items in Go.+

    Answer

    struct with unexported fields; constructor; methods return new Order; slices.Clone on items.

    Summary

    Structs compose data; embedding enables composition over inheritance. Exported fields (uppercase) are public API of the package. Struct tags drive JSON, DB, and validation serialization. Use constructors to enforce invariants on creation. Next lesson: methods — attaching behavior to types.

    Ready to mark this lesson complete?Track your journey across the entire course.