为什么 HTTP 处理程序的参数似乎指针反向?

huangapple go评论75阅读模式
英文:

Why http handler's arguments seem to have their pointer backward?

问题

我是新手,还在努力弄清楚一些事情。

为什么 w 不是一个指针,而 r 是一个指针呢?因为处理函数最终会向 w 写入数据,而只会从 r 中读取数据。

英文:

I am new to go and still trying to figure out a few things.

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

Why is w not a pointer and on the other hand r is, since the handler function will end up writing into w and only read from r?

答案1

得分: 2

这个问题已经在这个帖子中得到了回答,但为了简洁起见。

w http.ResponseWriter 实际上是一个由非导出指针支持的接口。

r *http.Request 是一个实际暴露的结构体。

我建议你点击上面的链接了解更多原因。

英文:

This question has already been answered in this post, but to keep it short.

w http.ResponseWriter is actually an interface that's backed by a non-exported pointer.

Whereas r *http.Request is an actual exposed struct.

I'd recommend following the above link to learn more why this is.

huangapple
  • 本文由 发表于 2016年3月26日 01:02:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/36224437.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定