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



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.



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.


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


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).


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


Was this article helpful to you? Yes No

How can we help?