pointers - 在 Go HTTP 处理程序中,为什么 ResponseWriter 是一个值,而 Request 是一个指针?

标签 pointers go

我正在通过为 GAE 编写应用程序来学习 Go,这是处理函数的签名:

func handle(w http.ResponseWriter, r *http.Request) {}

我在这里是指针新手,那么为什么 Request 对象是指针,而 ResponseWriter 不是?有没有必要这样,还是只是为了使某种基于指针的高级代码成为可能?

最佳答案

你得到的 w 是一个指向非导出类型 http.response 的指针,但由于 ResponseWriter 是一个接口(interface),它是不可见的.

来自 server.go :

type ResponseWriter interface {
    ...
}

另一方面,r 是指向具体结构的指针,因此需要显式传递引用。

来自 request.go :

type Request struct {
    ...
}

关于pointers - 在 Go HTTP 处理程序中,为什么 ResponseWriter 是一个值,而 Request 是一个指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13255907/

相关文章:

c - 结构链接表分段-故障

c - C 中 free() 的确切输出?

go - Go:如何使用gqlgen graphql实现禁用CORS?

go - 在 golang 的单独端口中公开 Prometheus 指标

c - 将值分配给动态分配的二维指针数组时出现语法错误

c - char * x,y,z;char* x,y,z;char (*)x,y,z; 和有什么区别?

c++ - 了解 C++ 中的指针初始化行为

mongodb - 从嵌套数组 golang mongodb 中删除元素

ubuntu - exec.Command() 的 Golang cmd.Output() 在 systemd 服务(ubuntu)中引发错误

xml - 如何在 GO 中解码灵活的 xml?