golang的`func (c *IPConn) Write(b []byte) (int, error)`返回什么?

huangapple go评论84阅读模式
英文:

What does golang's func (c *IPConn) Write(b []byte) (int, error)) return?

问题

我猜int是写入的字节数。

我认为该函数会阻塞,直到缓冲区完全写入到套接字或套接字关闭,所以我认为对于这个数字没有什么可做的(不像在C套接字中,我需要重试写入未写入的字节)。

我猜唯一可能返回的错误是写入失败,因为套接字已关闭?

在https://golang.org/pkg/net/#IPConn.Write文档中似乎没有提到这些内容,或者我找错了地方?

英文:

I guess that the int is the number of bytes written.

I think the function blocks until the buffer is fully written to the socket or the socket is closed so I think there's nothing to do be done with this number (unlike in a c socket where I would need to retry the write with the unwritten bytes).

I guess the only error that can be returned is in the event that the write fails because the socket is closed?

None of this seems to be in the documentation at https://golang.org/pkg/net/#IPConn.Write or am I looking in the wrong place?

答案1

得分: 2

这是io包的实现。import "io"导入了io包。

type Writer是一个接口,包装了基本的Write方法。

Write方法将p中的len(p)字节写入底层数据流。它返回从p中写入的字节数(0 <= n <= len(p))和导致写入提前停止的任何错误。如果返回的n < len(p),Write必须返回非nil错误。Write不能修改切片数据,即使是临时的。

实现不能保留p。

这是net包的实现。import "net"导入了net包。

type Conn是一个接口,表示通用的面向流的网络连接。

多个goroutine可以同时调用Conn上的方法。

func (*IPConn) Write是Conn接口的一个方法,它将数据写入连接。Write可以设置超时并在固定时间限制后返回一个带有Timeout() == true的错误;请参阅SetDeadline和SetWriteDeadline。

你可以在以下链接中找到更多信息:

英文:

> Package io
>
> import "io"
>
> type Writer
>
> type Writer interface {
> Write(p []byte) (n int, err error)
> }
>
> Writer is the interface that wraps the basic Write method.
>
> Write writes len(p) bytes from p to the underlying data stream. It
> returns the number of bytes written from p (0 <= n <= len(p)) and any
> error encountered that caused the write to stop early. Write must
> return a non-nil error if it returns n < len(p). Write must not modify
> the slice data, even temporarily.
>
> Implementations must not retain p.
>
> Package net
>
> import "net"
>
> type Conn
>
> type Conn interface {
> // Read reads data from the connection.
> // Read can be made to time out and return a Error with Timeout() == true
> // after a fixed time limit; see SetDeadline and SetReadDeadline.
> Read(b []byte) (n int, err error)
>
> // 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)
>
> // Close closes the connection.
> // Any blocked Read or Write operations will be unblocked and return errors.
> Close() error
>
> // LocalAddr returns the local network address.
> LocalAddr() Addr
>
> // RemoteAddr returns the remote network address.
> RemoteAddr() Addr
>
> // SetDeadline sets the read and write deadlines associated
> // with the connection. It is equivalent to calling both
> // SetReadDeadline and SetWriteDeadline.
> //
> // A deadline is an absolute time after which I/O operations
> // fail with a timeout (see type Error) instead of
> // blocking. The deadline applies to all future I/O, not just
> // the immediately following call to Read or Write.
> //
> // An idle timeout can be implemented by repeatedly extending
> // the deadline after successful Read or Write calls.
> //
> // A zero value for t means I/O operations will not time out.
> SetDeadline(t time.Time) error
>
> // SetReadDeadline sets the deadline for future Read calls.
> // A zero value for t means Read will not time out.
> SetReadDeadline(t time.Time) error
>
> // SetWriteDeadline sets the deadline for future Write calls.
> // Even if write times out, it may return n > 0, indicating that
> // some of the data was successfully written.
> // A zero value for t means Write will not time out.
> SetWriteDeadline(t time.Time) error
> }
>
> Conn is a generic stream-oriented network connection.
>
> Multiple goroutines may invoke methods on a Conn simultaneously.
>
> func (*IPConn) Write
>
> func (c *IPConn) Write(b []byte) (int, error)
>
> Write implements the Conn Write method.

It's an implementation of the io.Writer interface. Write writes len(p) bytes from p to the underlying data stream. It returns the number of bytes (n) written from p (0 <= n <= len(p)) and any > error (err) encountered that caused the write to stop early.

In particular, for the net.Conn interface, func (*IPConn) 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.

答案2

得分: -3

这是基于SetWriteDeadline的超时设置。如果超时并且已经写入了一些字节,你就知道写入了多少字节。

英文:

It is for the timeout based on SetWriteDeadline. If it times out and had written some bytes you know how many were written.

huangapple
  • 本文由 发表于 2016年3月11日 11:29:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/35931691.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定