6 ms·
The author of this blog post is a little confused about embedded structs. His examples of has-a and is-a are both has-a's, and the syntax change involved (leavi
by jnks 12y ago
The author of this blog post is a little confused about embedded structs. His examples of has-a and is-a are both has-a's, and the syntax change involved (leaving off a name for the embedded type) doesn't actually do anything.
type Person struct {
Name string
Address Address
}
is equivalent to
type Person struct {
Name string
Address
}
And in fact, these are equivalent too:
p.Address.Zip = "01313"
p.Zip = "01313"
http://play.golang.org/p/aKH3YxT5Mb http://play.golang.org/p/aKH3YxT5Mb
Go doesn't really support is-a for structs, as pointed out elsewhere in the comments here. Interface implementation is the only way to get the sort of "this type can be substituted for this other type" idea that is-a inheritance provides in other languages.