go - Assign value to parent golang structure field -


there 2 situations:

type struct {     a_field  string } type b struct {         b_field  string }  func main() {     b := &b{         a_field: "aaaa_field",         b_field: "bbbb_field",     }  } 

and

type struct {     a_field  string } type b struct {         b_field  string }  func main() {     b := &b{}     b.a_field = "aaaa_field"     b.b_field = "bbbb_field"     fmt.printf("good!") } 

why second 1 working, first 1 not? receive compile time exceptions. how should change first 1 work?

why second 1 working, first 1 not?

because

b.a_field = "aaaa_field" 

is actually

b.a.a_field = "aaaa_field" 

in disguise.

how should change first 1 work?

func main() {     b := &b{         a: a{             a_field: "aaaa_field",         },         b_field: "bbbb_field",     } } 

you should read how embedding work on effective go.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -