The more I think about this, the more I believe that this is a horrible 
situation that breaks all rules of encapsulation. This requires the creator to 
maintain references to the underlying Writer. It also requires the creator to 
be the sole decider of when the Writer is closed, or the creator must pass the 
concrete class to subordinates so they can close, or the subordinates must do 
type casting. 

The bufio.Writer is badly broken and should be changed to declare Close and 
pass this to the contained Writer if it is a WriterCloser. 

> On May 21, 2019, at 5:42 PM, Robert Engels <reng...@ix.netcom.com> wrote:
> 
> That’s a good case of dynamic type checking and good documentation
> 
>> On May 21, 2019, at 5:20 PM, roger peppe <rogpe...@gmail.com> wrote:
>> 
>> Probably it's not a Closer because that would imply that Close would close 
>> its underlying writer too, but then it would need to take a WriteCloser 
>> which would make it less general.
>> 
>>> On Tue, 21 May 2019, 19:02 Robert Engels, <reng...@ix.netcom.com> wrote:
>>> That’s what I was saying, I incorrectly thought that the Writer was a 
>>> WriterCloser.
>>> 
>>> Seems it should be with a noop if the contained Writer is not a 
>>> WriterCloser. This would be pretty much how every other platform does 
>>> buffered IO. 
>>> 
>>>> On May 21, 2019, at 11:19 AM, howardcs...@gmail.com wrote:
>>>> 
>>>> Excuse me if I am misunderstanding something - but it certainly looks to 
>>>> me like you are not at any point closing the bufio.Writer (because it is 
>>>> not a WriteCloser and does not support Close function). That is why you 
>>>> need to flush it! What you are closing is the underlying 
>>>> file/stream/WriteCloser, but it is the bufio.Writer that has the data in 
>>>> its buffer, *not* the file. This is not the same thing as flushing a file 
>>>> descriptor, which happens on close.
>>>> 
>>>> "After all data has been written, the client should call the Flush method 
>>>> to guarantee all data has been forwarded to the underlying io.Writer."
>>>> 
>>>> IF the bufio.Writer had been implemented as a WriteCloser, then closing it 
>>>> would probably flush as well - here is a discussion where they talk about 
>>>> how that could be implemented: 
>>>> https://stackoverflow.com/questions/43115699/how-to-get-a-bufio-writer-that-implements-io-writecloser
>>>> 
>>>> Howard
>>>> 
>>>>> On Tuesday, May 21, 2019 at 8:42:51 AM UTC-5, Subramanian Sridharan wrote:
>>>>> I don't think so.
>>>>> 
>>>>> When I close the file and don't explicitly flush:
>>>>> 
>>>>> package main
>>>>> 
>>>>> import (
>>>>>   "bufio"
>>>>>   "fmt"
>>>>>   "io"
>>>>>   "os"
>>>>> )
>>>>> 
>>>>> func main() {
>>>>>   sourceFilename := "MozillaFirefox-66.0.5-741.4.x86_64.rpm"
>>>>>   sourcef, err := os.Open(sourceFilename)
>>>>>   if err != nil {
>>>>>           fmt.Println("Error while opening source file:", err)
>>>>>           return
>>>>>   }
>>>>>   defer sourcef.Close()
>>>>> 
>>>>>   destinationFilename := "CopiedMozillaFirefox-66.0.5-741.4.x86_64.rpm"
>>>>>   os.Create(destinationFilename)
>>>>>   destf, err := os.OpenFile(destinationFilename, os.O_APPEND|os.O_WRONLY, 
>>>>> os.ModeAppend)
>>>>>   if err != nil {
>>>>>           fmt.Println("Error while opening destination file:", err)
>>>>>           return
>>>>>   }
>>>>>   defer func() {
>>>>>           fmt.Println("Closing file.")
>>>>>           destf.Close()
>>>>>   }()
>>>>> 
>>>>>   fReader := bufio.NewReader(sourcef)
>>>>>   fWriter := bufio.NewWriter(destf)
>>>>> 
>>>>>   n, err := io.Copy(fWriter, fReader)
>>>>>   if err != nil {
>>>>>           fmt.Println("Error while copying:", err)
>>>>>           return
>>>>>   }
>>>>> 
>>>>>   fmt.Println("Copied", n, "bytes.")
>>>>> }
>>>>> 
>>>>>> 
>>>> 
>>>> -- 
>>>> 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/24d5242a-3e5a-4b83-a6f2-1ca7e29cfe0e%40googlegroups.com.
>>>> For more options, visit https://groups.google.com/d/optout.
>>> 
>>> -- 
>>> 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/D61080CC-6271-4782-88A8-9213A5C12A95%40ix.netcom.com.
>>> For more options, visit https://groups.google.com/d/optout.
>> 
>> -- 
>> 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/CAJhgacidQJLbjQ16BBDLOQ57q5BAbqJvu1Y57-itoAV9DtPDLQ%40mail.gmail.com.
>> For more options, visit https://groups.google.com/d/optout.

-- 
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/2E5FCA89-0825-4BBD-910F-CBF55A4ECD29%40ix.netcom.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to