https://golang.org/src/net/http/server.go#L139
我希望签名是 Write([]byte) error
,而不是 Write([]byte) (int, error)
。通过查看用法,我也找不到任何好的解释,并且文档注释没有解释返回值。
返回的int
是什么意思?
最佳答案
ResponseWriter.Write()
方法是实现一般io.Writer
接口(interface),因此可以使用/传递 http.ResponseWriter
的值到任何接受/在 io.Writer
上工作的实用程序函数。
io.Writer
只有一个 Write()
方法,它详细说明了 Write
的“契约”、它应该返回什么以及它应该如何工作:
type Writer interface {
Write(p []byte) (n int, err error)
}
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).
关于http - ResponseWriter.Write 返回的 int 是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61713311/