I have multiple Goroutines sharing a net.Conn object. Can they issue a Write calls simultaneously?
My main concern is that of Write calls which are partially done. Say I intend to wrote 100 bytes, but only 30 were sent, so I need to send 70 more. For this I will typically write a loop:
count := 0
for count < len(buf) {
byteSent, err := conn.Write(buf[count:])
//check error
count += byteSent
}
But I see that Go implements this loop in net.Conn.Write line number 318 and it does so by taking a lock.
However, on Windows implementation there is no such loop except that there is a call to WSASend. I do not know how WSASend behaves and could not get much from the MSDN docs
Hence the questions are:
[edit] Added 4th question
- Do I need to acquire a lock everytime I write to socket?
- If yes, then the purpose of acquiring the lock in Write implementation is defeated.
- In unix implementation, does it mean that I cannot get byteSent < len(buf) unless err != nil? (I mean am I reading the code correct?)
- Does the WSASend on Windows implements the equivalent loop in Unix implementation