Go: 读取 http.Request 中的方法

标签 go

我正在阅读 Sau Sheong Chang 的Go Web Programming。以下是从请求正文中读取数据的示例代码:

import (
    "fmt"
    "net/http"
)

func bodyfunc(w http.ResponseWriter, r *http.Request) {
    len := r.ContentLength
    body := make([]byte, len)
    r.Body.Read(body)
    fmt.Fprintln(w, string(body))
}

func main() {
    server := http.Server{
        Addr: "127.0.0.1:8080",
    }
    http.HandleFunc("/body", bodyfunc)
    server.ListenAndServe()
}

根据定义,Request 结构中的 Body 字段实际上是一个 io.ReadCloser 接口(interface)。我的问题是:这个接口(interface)中的Read方法只是声明了没有实现。同时代码运行良好。 Read 方法的实现一定是在某个地方完成的。它在哪里?

最佳答案

实现在 https://golang.org/src/net/http/transfer.go#L637 中. 我用了delve调试器找到它。

关于Go: 读取 http.Request 中的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35500854/

相关文章:

security - 加密 key 的安全持久性和 IPC

go - Golang 上传文件的 Mime 类型检查

go - strings.Split,标准输入不返回 slice/数组

go - 如何在html文件中编写golang代码(gin gonic框架)

go - 为什么关闭sftp客户端不会关闭整个SSH连接

api - 伊姆古尔 API : POST comment in GO

function - 如何使用 interface{} 作为灵活的函数参数和返回类型?

go - 停止 goroutine 中的所有递归函数

xml - 使用 Unmarshal 在 Go 中获取 XML 命名空间前缀

postgresql - Go Hood 保存到 Postgres,但不确定在哪里