Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang embedded struct type

I have these types:

type Value interface{}

type NamedValue struct {
    Name  string
    Value Value
}

type ErrorValue struct {
    NamedValue
    Error error
}

I can use v := NamedValue{Name: "fine", Value: 33}, but I am not able to use e := ErrorValue{Name: "alpha", Value: 123, Error: err}

Seems that embedding syntax was ok, but using it doesn't work?

like image 720
Ayman Avatar asked Jul 19 '17 11:07

Ayman


People also ask

What is struct embedding Golang?

Go by Example: Struct EmbeddingGo supports embedding of structs and interfaces to express a more seamless composition of types. This is not to be confused with //go:embed which is a go directive introduced in Go version 1.16+ to embed files and folders into the application binary.

What is embedded type in Golang?

From the article structs in Go, we know that a struct type can have many fields. Each field is composed of one field name and one field type. In fact, sometimes, a struct field can be composed of a field type only. The way to declare struct fields is called type embedding.

What is embedded 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.

What is embedded interface in Golang?

In Go language, the interface is a collection of method signatures and it is also a type means you can create a variable of an interface type. As we know that the Go language does not support inheritance, but the Go interface fully supports embedding.

What is a struct in Golang?

A struct (short for "structure") is a collection of data fields with declared data types. Golang has the ability to declare and create own data types by combining one or more types, including both built-in and user-defined types. Each data field in a struct is declared with a known type, which could be a built-in type or another user-defined type.

How do you declare a data type in Golang?

Golang has the ability to declare and create own data types by combining one or more types, including both built-in and user-defined types. Each data field in a struct is declared with a known type, which could be a built-in type or another user-defined type.

What are some examples of struct-in-struct embedding in go?

A classical example of struct-in-struct embedding in Go is sync.Mutex . Here's lruSessionCache from crypto/tls/common.go:

What are the different types of structs in go?

Types of Structs in Go 1 Named struct#N#A named struct is any struct whose name has been declared before. So, it can be initialized using its... 2 Anonymous struct More ...


2 Answers

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.

like image 96
icza Avatar answered Sep 25 '22 13:09

icza


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"
like image 27
Federico Avatar answered Sep 24 '22 13:09

Federico