io.MultiWriter has an extra check to bytes written

136 views
Skip to first unread message

stinkin...@gmail.com

unread,
Sep 25, 2016, 3:22:55 PM9/25/16
to golang-nuts
I was fiddling around and tried implementing my own MultiWriter and after a while I was satisfied that it was an exact replica of io.MultiWriter until I actually looked:

https://golang.org/src/io/multi.go line 55, if n != len(p)

Why is the check even there?
As I understand, the contract of the interface Writer specifically says:
Write must return a non-nil error if it returns n < len(p)

Since the check to err != nil precedes n != len(p), any proper Writer should never encounter a situation where the length check is false.
Is this an intentional "fix" to rogue Writers out there?
Or is this redundant and should just be changed?
And since if n > len(p) the error shouldn't be ErrShortWrite anyways so I'd think it rules out the possibility of fixing some Writer writing more bytes than len(p).

thebroke...@gmail.com

unread,
Sep 26, 2016, 1:13:58 PM9/26/16
to golang-nuts, stinkin...@gmail.com
That code was written in 2010, while the specification for io.Writer was written in 2009. Go 1.0 was released in 2012. Prior to Go 1.0, there wasn't any strict compatibility agreement, so many things were in flux throughout the code base. So, I assume that Brad added that check back then to handle "rogue Writers" that may already be in existence back then or in case the io.Writer specification changed.

I wouldn't worry about adding that check today.
Reply all
Reply to author
Forward
0 new messages