在Go HTTP处理程序中,为什么ResponseWriter是一个值,但Request是一个指针? [英] In Go HTTP handlers, why is the ResponseWriter a value but the Request a pointer?

查看:1728
本文介绍了在Go HTTP处理程序中,为什么ResponseWriter是一个值,但Request是一个指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  func句柄

我通过编写一个GAE应用程序来学习Go,这是一个处理函数的签名。 (w http.ResponseWriter,r * http.Request){}

我是这里的指针新手,那为什么 Request 对象是一个指针,但是 ResponseWriter 不是?有没有必要这样做,或者这只是为了使某种基于高级指针的代码成为可能? 解决方案

你得到的是 w 是一个指向非导出类型 http.response 但是指向 ResponseWriter的指针是一个界面,不可见。



来自 server.go

 类型ResponseWriter接口{
...

另一方面, r 是一个指向具体结构体的指针,因此需要明确指出:

From request.go

 类型请求结构{
...
}


I'm learning Go by writing an app for GAE, and this is signature of a handler function:

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

I'm pointer newbie here, so why is the Request object a pointer, but the ResponseWriter not? Is there any need to have it this way or is this just to make some kind of advanced pointer based code possible?

解决方案

What you get for w is a pointer to the non exported type http.response but as ResponseWriter is an interface, that's not visible.

From server.go :

type ResponseWriter interface {
    ...
}

On the other hand, r is a pointer to a concrete struct, hence the need to precise it explicitely :

From request.go :

type Request struct {
    ...
}

这篇关于在Go HTTP处理程序中,为什么ResponseWriter是一个值,但Request是一个指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆