Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mixture of field:value and value initializers

Tags:

go

Why can I not create the following, with an anonymous field?

type T1 struct {     T1_Text string }  type T2 struct {     T2_Text string     T1 } 

used in func ..

t := T2{     T2_Text: "Test",     T1{T1_Text: "Test"}, } 

Gives me: mixture of field:value and value initializers?

like image 517
Chris G. Avatar asked Dec 23 '14 19:12

Chris G.


1 Answers

A brief explanation.

The reason you get that is because you are allowed to only use one of the two types of initializers and not both.

i.e. you can either use field:value or value.

Using your example you either do

field:value

t := T2{     T2_Text: "Test",     T1: T1{T1_Text: "Test"}, } 

or only values

t := T2{     "Test",     T1{"Test"}, } 

Hope that explains the why

like image 189
Dee S Avatar answered Sep 28 '22 09:09

Dee S