I have implemented a custom Write interface for my cloud program. My problem so far is that after i am done copying files to my writer and closed the Writer, the writer still has a few Writes to do(usually maybe 4 writes about 4096 bytes each). The last Write is usually less than 4096.
This has not happened yet but i know it is a probability of 1/4096 that the last Write is 4096 bytes and my program won't terminate.
I am using this for a zipping program and io.EOF is not effective as every write chunk has one, also checking if writer is closed comes too early while there are still some writes to do.
What is the best way to handle this situation?
***EDIT***** I ended up implementing a more Robust Write(), Flush() and Close() method.Now everything is good if i use defer Close() but i still get the same problem if i manually call Close() at the end
since you have full control on the writer, you could use a waitgroup to wait in your main
for all goroutines to finish.
Problem was solved by implementing a more robust Close()
function. I also used defer Close()
to make sure that Golang handled all the Goroutines internally.