hey
hey

Reputation: 7859

How to define embedded /anonymous fields (go struct )

I'm trying to initialize an embedded struct. However the compiler says I can't mix values and and value initializers. What's the correct syntax ? httpCl is of type *requests.Trans

type clTran struct {
    *requests.Trans
    uCh chan user
}

func main() {
    httpCl, err := requests.tr(px)
    clT := clTran{httpCl, uCh: uCh}
}

Upvotes: 2

Views: 691

Answers (1)

Linear
Linear

Reputation: 22236

If you label fields in a struct literal (which you usually should) all of them need to be labeled. In the case of embedding, the field takes the name of its type. So

clT := clTran {
    Trans: httpCl,
    uCh: uCh,
}

Note that this field name applies to accessing and writing too, clT.Trans = httpCl is valid and will write to the embedded field.

Upvotes: 4

Related Questions