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?
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.
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.
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.
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.
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.
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.
A classical example of struct-in-struct embedding in Go is sync.Mutex . Here's lruSessionCache from crypto/tls/common.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 ...
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
, andT
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With