1. Home
  2. Docs
  3. golang
  4. 内置库(包-package)
  5. io

io

Functions

func WriteString

func WriteString(w Writer, s string) (n int, err error)


WriteString writes the contents of the string s to w, which accepts a slice of bytes. If w implements StringWriter, its WriteString method is invoked directly. Otherwise, w.Write is called exactly once.

WriteString将字符串s的内容写到w,w接受一个字节片。如果w实现了StringWriter,它的WriteString方法被直接调用。否则,w.Write正好被调用一次。

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.

Writer是包装基本Write方法的接口。

Write将len(p)字节从p写到底层数据流中。它返回从p写入的字节数(0 <= n <= len(p)),以及遇到的导致写入提前停止的任何错误。如果Write返回n < len(p),它必须返回一个非零的错误。写入时不能修改分片数据,即使是暂时的。

io.Copy

Copy copies from src to dst until either EOF is reached on src or an error occurs. It returns the number of bytes copied and the first error encountered while copying, if any.

A successful Copy returns err == nil, not err == EOF. Because Copy is defined to read from src until EOF, it does not treat an EOF from Read as an error to be reported.

If src implements the WriterTo interface, the copy is implemented by calling src.WriteTo(dst). Otherwise, if dst implements the ReaderFrom interface, the copy is implemented by calling dst.ReadFrom(src).

拷贝从src到dst,直到src上达到EOF或发生错误。它返回复制的字节数和复制时遇到的第一个错误(如果有)。

一个成功的Copy返回err == nil,而不是err == EOF。因为Copy被定义为从src读到EOF,所以它不把Read的EOF当作要报告的错误。

如果src实现了WriterTo接口,则通过调用src.WriteTo(dst)实现拷贝。否则,如果dst实现了ReaderFrom接口,那么就通过调用dst.ReadFrom(src)来实现拷贝。

Was this article helpful to you? Yes No

How can we help?