I understand that interface does not provide the implementation. Perhaps my 
question was not clear. Let me try with the code snippets.

This is how http.ResponseWriter is written:
type http.ResponseWriter interface {
    ...
    Write([]byte) (int, error)
    ...
}

But it could have also been written this way:
type http.ResponseWriter interface {
    ...
    io.Writer
    ...
}

The second one uses embedding (https://go.dev/doc/effective_go#embedding), 
which I thought is preferable. I was curious why embedding was not used 
here and more broadly, when not to use embedding.

Thanks
Nitin

On Friday, April 28, 2023 at 2:45:19 PM UTC-7 Aaron Rubesh wrote:

> I would recommend reading up on how to implement Go interfaces.
>
> The only requirement required to satisfy the io.Writer interface is a 
> method Write([]byte)(int,error). If this method is present, Go can then 
> interpret your struct as a io.Writer.
>
> By itself, io.Writer provides no functionality only a definition. That is 
> left to the struct to fulfill. 
> On Friday, April 28, 2023 at 4:16:24 PM UTC-5 Nitin Muppalaneni wrote:
>
>> Is there a reason why `http.ResponseWriter` does not include io.Writer 
>> directly and instead has the `Write([]byte) (int, error)` method? Why did 
>> you choose to do it this way? I can see that it adds a dependency on the io 
>> package. Was that the concern?
>>
>> And more generally, when would you recommend not using composition of 
>> interfaces?
>>
>> Thanks
>> Nitin
>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/c83c6125-31f2-49bf-b4c9-d67ab0652375n%40googlegroups.com.

Reply via email to