http - ResponseWriter.Write 返回的 int 是什么意思?

标签 http go

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/

相关文章:

ruby - 加载网页时 : "undefined method ` request_uri' for #"

go - 如何检查嵌套结构中是否存在键

.htaccess - 网站不断重定向到 HTTP

c# - 使用 JsonTextReader 值作为新流传递 Base64 编码的字符串

go - 语义 - 将外部接口(interface)类型值传递给内部接口(interface)变量

go - 如何查找和替换报价中的内容?

go - golang 中的事件驱动模式

json - 有没有更简单的方法使用 Golang JSON 编码在 JSON 对象上添加层?

c# - 如何在 .NET 中下载大文件(通过 HTTP)?

http - Google API - 从 Oauth2 请求 token 返回 "invalid_request"