Or do I only need to check is the returned err var nil or not and ignore the returned length ?
Or do I only need to check is the returned err var nil or not and ignore the returned length ?The io.Writer contract specifies that if less than len(buf) bytes are written, an error must be returned.Critically it does _not_ make any guarantees about the content or form of that error message.
Go will write all the bytes you want, except in case of error.
In essence: go runtime includes the for loop.
It depends. There will be an error if written length is less than the wanted. You have to decide what to do based on the error and your goals.
// Write writes data to the connection. // Write can be made to time out and return a Error with Timeout() == true // after a fixed time limit; see SetDeadline and SetWriteDeadline. Write(b []byte) (n int, err error)
So the Write call return an error var, and what does this error var mean?
How can I know what kind of error is?
After search the web, I found that this err var maybe implement the net.Error interface or maybe an net.OpError struce, but what message can I got from this interface or strut?
Take the net.Error interface for example, this is the definition of the interface:
type Error interface { error Timeout() bool // Is the error a timeout? Temporary() bool // Is the error temporary? }
From the interface, I can know that if the err var implements this interface, I can use the Timeout() or Temporary() func to know if the error is an timeout error or an temporary error.
I know what timeout means, but what does temporary error means?
Does it means I can call the Write call again? Where can I found the definition of Temporary?
And what about oter kind of error? Like the connection closed by the peer, how can I know this type of error?
And for net.OpError, this is the definition:
type OpError struct { // Op is the operation which caused the error, such as // "read" or "write". Op string // Net is the network type on which this error occurred, // such as "tcp" or "udp6". Net string // Addr is the network address on which this error occurred. Addr Addr // Err is the error that occurred during the operation. Err error }
Again, from the comments of the definition, I don't know how to find the exact type of the error.
Do I have to use regex to find the pattern of the error string to find out what kind of error it is?
And I notice that the error can also be an syscall.Errno type, is this type enough for distinguish the error type?
And which func in the net package will return this type of error? Is there any document I can read from ?
thanks
On Thu, Oct 9, 2014 at 9:11 AM, Xiaobin She <xiaob...@gmail.com> wrote:
>
>
> 在 2014年10月9日星期四UTC+8下午2时27分01秒,Tamás Gulácsi写道:
>>
>> It depends. There will be an error if written length is less than the
>> wanted. You have to decide what to do based on the error and your goals.
>
>
>
> when the return length is less than len(buf), what I want is:
> 1. if the err indicates that I can call the write again, than I will call
> the write again to send the reset of the data
> for example, in C code, if the errno is EAGAIN when using non-blocking IO, I
> will know that I can call the write system call again
> Is there some err like this one ?
You cannot observe Go's use of non blocking IO from a Go program. From
the POV of a single goroutine Read or Write _always_ blocks.
>
> 2. if the err indicates that it's some kind of other error (like the
> connection is closed by the peer), than I will close the connection and
> return fail
What I and the other contributors are trying to say is you have two states
1. err == nil, everything worked
2. err != nil, something failed, that's all we know
You _could_ try to use a type assertion or type switch to interrogate
the value inside the error, but you'll find that few packages give any
suggestion of what error will returned.
As I said the for cycle which retries writing where possible (the error is not an error, or eventually it will succeed), is in the Go runtime (for sockets).
So you won't get EAGAIN.