2 回答

TA貢獻(xiàn)1859條經(jīng)驗(yàn) 獲得超6個(gè)贊
包io
import "io"
類型作家
type Writer interface {
Write(p []byte) (n int, err error)
}
Writer 是封裝了基本 Write 方法的接口。
Write 將 len(p) 個(gè)字節(jié)從 p 寫(xiě)入底層數(shù)據(jù)流。它返回從 p (0 <= n <= len(p)) 寫(xiě)入的字節(jié)數(shù)以及導(dǎo)致寫(xiě)入提前停止的任何錯(cuò)誤。如果 Write 返回 n < len(p),則它必須返回一個(gè)非 nil 錯(cuò)誤。寫(xiě)入不得修改切片數(shù)據(jù),即使是臨時(shí)修改。
實(shí)現(xiàn)不能保留 p。
包網(wǎng)
導(dǎo)入“網(wǎng)”
類型連接
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 是一個(gè)通用的面向流的網(wǎng)絡(luò)連接。
多個(gè) goroutine 可以同時(shí)調(diào)用 Conn 上的方法。
func (*IPConn) 寫(xiě)
func (c *IPConn) Write(b []byte) (int, error)
Write 實(shí)現(xiàn)了 Conn Write 方法。
它是 io.Writer 接口的實(shí)現(xiàn)。Write 將 len(p) 個(gè)字節(jié)從 p 寫(xiě)入底層數(shù)據(jù)流。它返回從 p (0 <= n <= len(p)) 寫(xiě)入的字節(jié)數(shù) (n) 以及遇到導(dǎo)致寫(xiě)入提前停止的任何 > 錯(cuò)誤 (err)。
特別是,對(duì)于 net.Conn 接口, func (*IPConn) Write 將數(shù)據(jù)寫(xiě)入連接??梢允箤?xiě)入超時(shí)并在固定時(shí)間限制后使用 Timeout() == true 返回錯(cuò)誤;請(qǐng)參閱 SetDeadline 和 SetWriteDeadline。

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超8個(gè)贊
它用于基于 SetWriteDeadline 的超時(shí)。如果超時(shí)并寫(xiě)入了一些字節(jié),您就會(huì)知道寫(xiě)入了多少字節(jié)。
- 2 回答
- 0 關(guān)注
- 549 瀏覽
添加回答
舉報(bào)