Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

An anonymous struct in Go is a composite data type defined without an explicit type identifier. While often declared and instantiated simultaneously within a single expression, an anonymous struct type can also be used purely as a variable’s type declaration or as a field type within another named struct definition.

Syntax and Instantiation

An anonymous struct is defined using the struct keyword followed by field definitions enclosed in braces. It can be declared as a type for a variable without immediate initialization, or it can be instantiated inline by appending a second set of braces containing the initialization values.
package main

import "fmt"

func main() {
    // Declared without instantiation
    var uninitialized struct {
        Count int
    }
    uninitialized.Count = 5

    // Declared and instantiated inline
    initialized := struct {
        Field1 string
        Field2 int
    }{
        Field1: "value1",
        Field2: 42,
    }

    fmt.Println(uninitialized)
    fmt.Println(initialized)
}

Type Identity and Assignability

Unlike named structs, which are strictly typed by their declared identifier (nominal typing), anonymous structs rely on structural equivalence. Two anonymous structs are considered to be of the exact same type if their underlying types are identical. This requires that they possess the exact same sequence of fields, and that corresponding fields have identical names, identical types, and identical struct tags. Because of this structural equivalence, you can assign one anonymous struct to another, or assign an anonymous struct to a named struct, provided their underlying types are identical.
package main

import "fmt"

type User struct {
    ID   int
    Name string
}

func main() {
    // Anonymous struct A
    a := struct {
        ID   int
        Name string
    }{ID: 1, Name: "Alice"}

    // Anonymous struct B
    b := struct {
        ID   int
        Name string
    }{ID: 2, Name: "Bob"}

    // Valid: Both share identical underlying types
    a = b

    // Valid: Anonymous struct 'a' is implicitly convertible to named type 'User'
    var u User = a

    fmt.Println(a)
    fmt.Println(b)
    fmt.Println(u)
}

Pointers to Anonymous Structs

To allocate an anonymous struct and immediately obtain a pointer to it, prepend the address-of operator (&) to the struct keyword during instantiation.
package main

import "fmt"

func main() {
    ptr := &struct {
        Status int
        Active bool
    }{
        Status: 200,
        Active: true,
    }
    
    // ptr is of type: *struct { Status int; Active bool }
    fmt.Printf("Type: %T, Value: %v\n", ptr, ptr)
}

Embedding and Tags

Anonymous structs support all standard struct mechanics. They can contain embedded (anonymous) fields for composition and utilize struct tags for reflection-based operations.
package main

import (
    "fmt"
    "sync"
)

func main() {
    complexStruct := struct {
        sync.Mutex                  // Embedded field
        Payload    []byte `json:"data"` // Struct tag
    }{
        Payload: []byte("raw_data"),
    }

    complexStruct.Lock()
    complexStruct.Payload = []byte("mutated_data")
    complexStruct.Unlock()

    fmt.Printf("Payload: %s\n", complexStruct.Payload)
}

Arrays and Slices of Anonymous Structs

Anonymous structs can be used as the element type within composite literals for slices or arrays. The struct definition is declared once in the slice signature, and the individual elements are instantiated using only the initialization braces.
package main

import "fmt"

func main() {
    list := []struct {
        Key   string
        Value int
    }{
        {Key: "A", Value: 100},
        {Key: "B", Value: 200},
    }

    fmt.Println(list)
}
Master Go with Deep Grasping Methodology!Learn More