I have these types:
type Value interface{}
type NamedValue struct {
Name string
Value Value
}
type ErrorValue struct {
NamedValue
Error error
}
I can use 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?
Embedded types are (unnamed) fields, referred to by the unqualified type name.
Spec: Struct types:
So try:
Also works if you omit the field names in the composite literal:
Try the examples on the Go Playground.
In addition to the wonderful answer by icza.
you can simply do this:
and it works just fine. checkout the example Here
For deeply nested structs, the accepted answer's syntax is a little verbose. For example, this :
(Go playground)
Could be rewritten like this: