[Go] Use an anonymous struct

Created on November 30, 2026

Declare a struct type directly in a composite literal when you only need the type there. This example defines Name and Age fields, initializes them, and prints user.Name.

package main

import "fmt"

func main() {
    user := struct {
        Name string
        Age  int
    }{
        Name: "Yamada",
        Age:  10,
    }

    fmt.Println(user.Name)
}
Yamada
Go