Earlier quoted context omitted.
It looks more like inheritance: the methods on net.Conn & io.Writer are automatically exposed. Are you saying that it is composition because you are "inheriting" from interfaces, which isn't traditional inheritance? Or if not, can you explain why you think of this as composition and not inheritance? Edit: On further thought, this looks to me like composition with automatic delegation (a huge productivity benefit of i…
The inner structs (of which the outer writer struct is composed) cannot access any of the functionality or fields outside of itself. Each inner struct is completely encapsulated, so you can't override an inner struct method and expect the inner struct's behavior to change. The accessibility of inner struct methods from the outer struct is a syntactic convenience. Java: https://gist.github.com/jaekwon/8025b9f3a482b321…
Struct composition with Go
11–20 of 20 posts
Re: Struct composition with Go
#12This is cool, but I find this snippet a tad bizarre: client, server := net.Pipe() var buf bytes.Buffer client = &recordingConn { Conn: client, Writer: io.MultiWriter(client, &buf), } While it seemingly works, it seems confusing to assign client to an internal field of a new struct, and then overwriting that same client variable with the newly created struct...not to mention passing it into MultiWriter. Anyway, I like…
What is the benefit of re-using the 'client' identifier? There's no closure capturing the identifier. Compare: client, server := net.Pipe() var buf bytes.Buffer client = &recordingConn { Conn: client, Writer: io.MultiWriter(client, &buf), } vs tempClient, server := net.Pipe() var buf bytes.Buffer client = &recordingConn { Conn: tempClient, Writer: io.MultiWriter(tempClient, &buf), } The latter is less confusing to me…
Re: Struct composition with Go
#13Earlier quoted context omitted.
Nope, this is composition, not inheritance.
It looks more like inheritance: the methods on net.Conn & io.Writer are automatically exposed. Are you saying that it is composition because you are "inheriting" from interfaces, which isn't traditional inheritance? Or if not, can you explain why you think of this as composition and not inheritance? Edit: On further thought, this looks to me like composition with automatic delegation (a huge productivity benefit of i…
ex
type A struct{}
func (a *A)GetSelf()*A{return a}
type B struct{ A }
b:=&B{A{}}
b.GetSelf() returns type A , not type BIt gets even more confusing when you start embedding interfaces in structs.
Re: Struct composition with Go
#14Re: Struct composition with Go
#15Re: Struct composition with Go
#16This is cool, but I find this snippet a tad bizarre: client, server := net.Pipe() var buf bytes.Buffer client = &recordingConn { Conn: client, Writer: io.MultiWriter(client, &buf), } While it seemingly works, it seems confusing to assign client to an internal field of a new struct, and then overwriting that same client variable with the newly created struct...not to mention passing it into MultiWriter. Anyway, I like…
What is the benefit of re-using the 'client' identifier? There's no closure capturing the identifier. Compare: client, server := net.Pipe() var buf bytes.Buffer client = &recordingConn { Conn: client, Writer: io.MultiWriter(client, &buf), } vs tempClient, server := net.Pipe() var buf bytes.Buffer client = &recordingConn { Conn: tempClient, Writer: io.MultiWriter(tempClient, &buf), } The latter is less confusing to me…
Re: Struct composition with Go
#17Re: Struct composition with Go
#18Earlier quoted context omitted.
Nope, this is composition, not inheritance.
I would say that it's inheritance without polymorphisms or a sort of mixin.
Re: Struct composition with Go
#19Earlier quoted context omitted.
Nope, this is composition, not inheritance.
I would say that it's inheritance without polymorphisms or a sort of mixin.
Inheritance implies is-a, and implies that you can modify some of the internal workings of the class you're inheriting from.
Embedding is strictly has-a. It's really no different than having a member variable in any other language... there's just some syntactic sugar to make it a little nicer to access the member's methods and fields (and the methods "count" for fulfilling interfaces).
Re: Struct composition with Go
#20Since the purpose of this is apparently to provide a fake structure to test against - would another viable approach have been to implement a trivial `Close` function that doesn't do anything? (context: i am no gopher) (edit/tangent: i wonder how method calls from inside method calls are resolved when the names are ambiguous?)
I tried to write an example that demonstrates why I don't think the ambiguity you're referring to is possible.