dts777777 2012-11-06 17:07
浏览 396
已采纳

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

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?

  • 写回答

5条回答 默认 最新

  • doufei2007 2012-11-06 17:08
    关注

    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 {
        ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突