Golang embedded struct type

Embedded types are (unnamed) fields, referred to by the unqualified type name.

Spec: Struct types:

A field declared with a type but no explicit field name is an anonymous field, also called an embedded field or an embedding of the type in the struct. An embedded type must be specified as a type name T or as a pointer to a non-interface type name *T, and T itself may not be a pointer type. The unqualified type name acts as the field name.

So try:

e := ErrorValue{NamedValue: NamedValue{Name: "fine", Value: 33}, Error: err}

Also works if you omit the field names in the composite literal:

e := ErrorValue{NamedValue{"fine", 33}, err}

Try the examples on the Go Playground.


For deeply nested structs, the accepted answer's syntax is a little verbose. For example, this :

package main

import (
    "fmt"
)

type Alternative struct {
    Question
    AlternativeName string
}

type Question struct {
    Questionnaire
    QuestionName  string
}

type Questionnaire struct {
    QuestionnaireName string
}

func main() {
    a := Alternative{
        Question: Question{
            Questionnaire: Questionnaire{
                QuestionnaireName: "q",
            },
        },
    }
    fmt.Printf("%v", a)
}

(Go playground)

Could be rewritten like this:

a := Alternative{}
a.QuestionnaireName = "q"