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
Post a Comment