在 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 ?
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
And what confuse me is that I don't know how to distinguish these two type
of error.
I found it difficult to find the return err message from the api page, from
the official website, all I can found is this:
// 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 <
http://golang.org/pkg/builtin/#byte>) (n int <
http://golang.org/pkg/builtin/#int>, err error <
http://golang.org/pkg/builtin/#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 <
http://golang.org/pkg/builtin/#error>
Timeout() bool <
http://golang.org/pkg/builtin/#bool> // Is the error a timeout?
Temporary() bool <
http://golang.org/pkg/builtin/#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 <
http://golang.org/pkg/builtin/#string>
// Net is the network type on which this error occurred,
// such as "tcp" or "udp6".
Net string <
http://golang.org/pkg/builtin/#string>
// Addr is the network address on which this error occurred.
Addr Addr <
http://golang.org/pkg/net/#Addr>
// Err is the error that occurred during the operation.
Err error <
http://golang.org/pkg/builtin/#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
--
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+unsubscribe@googlegroups.com.
For more options, visit
https://groups.google.com/d/optout.