Built-in Golang Structure Type

I have the following 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 can not usee := ErrorValue{Name: "alpha", Value: 123, Error: err}

The injection syntax seems to be good, but using it doesn't work?

+18
source share
2 answers

Inline types are (unnamed) fields referenced by an unqualified type name.

Spec: Structure Types:

, , , , . T *T, T . .

, :

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

, :

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

Go Playground.

+27

icza.

:

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

.

+1

Source: https://habr.com/ru/post/1681781/


All Articles