> ## 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.

# Go Struct Literal

A struct literal in Go is a composite literal expression used to allocate and initialize a new instance of a struct type in a single operation. It constructs the struct value in memory and binds specified values to its fields during instantiation.

## Syntax and Mechanics

Given a defined struct type:

```go theme={"dark"}
type ServerConfig struct {
    Host string
    Port int
    TLS  bool
}
```

Go supports several syntactic forms for struct literals, each with specific initialization rules.

### 1. Named Field Initialization

This is the idiomatic approach, where values are explicitly mapped to field names.

```go theme={"dark"}
config := ServerConfig{
    Host: "localhost",
    Port: 8080,
}
```

* **Order Independence:** Fields can be initialized in any order.
* **Partial Initialization:** Any field omitted from the literal is automatically initialized to its type's respective zero value (e.g., `TLS` becomes `false`).

### 2. Positional Initialization

Values are assigned based strictly on the order of the fields defined in the struct type declaration.

```go theme={"dark"}
config := ServerConfig{"localhost", 8080, true}
```

* **Strict Arity:** You must provide a value for *every* field in the struct. You cannot omit values to rely on zero-value initialization.
* **Order Dependence:** The sequence of values must exactly match the struct's memory layout.

### 3. Empty Literal (Zero-Value Instantiation)

An empty struct literal initializes all fields to their respective zero values.

```go theme={"dark"}
config := ServerConfig{}
```

### 4. Pointer to Struct Literal

Prefixing the struct literal with the address-of operator (`&`) allocates the struct on the heap (if it escapes the local scope) or stack, initializes it, and returns a pointer to the newly allocated memory (`*ServerConfig`).

```go theme={"dark"}
configPtr := &ServerConfig{
    Host: "127.0.0.1",
}
```

This is syntactically equivalent to using the built-in `new()` function followed by field assignment, but it is executed as a single expression.

### 5. Anonymous Struct Literal

A struct literal can be declared and instantiated simultaneously without binding the struct definition to a named type identifier.

```go theme={"dark"}
inlineConfig := struct {
    Timeout int
    Retries int
}{
    Timeout: 30,
    Retries: 3,
}
```

## Lexical Formatting Rules

Due to Go's automatic semicolon insertion (ASI) rules, multi-line struct literals enforce strict comma placement. A trailing comma is mandatory after the final field assignment if the closing brace `}` is on a new line.

**Valid:**

```go theme={"dark"}
config := ServerConfig{
    Host: "localhost",
    Port: 8080, // Trailing comma required
}
```

**Valid:**

```go theme={"dark"}
config := ServerConfig{Host: "localhost", Port: 8080} // No trailing comma needed
```

**Compilation Error:**

```go theme={"dark"}
config := ServerConfig{
    Host: "localhost",
    Port: 8080 // Syntax error: missing ',' before newline
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Go Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
