ResponseWriter interface in Go net/http package

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

ResponseWriter interface in Go net/http package

问题

我对net/http包中的ResponseWriter接口有两个问题。以下是我的代码:

package main

import (
	"fmt"
	"net/http"
	"os"
)

type handler struct{}

func (h *handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	fmt.Fprintf(w, "Method: %v\n", req.Method)
	fmt.Fprintf(w, "URL: %v\n", req.URL)
	fmt.Fprintf(w, "Protocol: %v\n", req.Proto)

	fmt.Fprintf(os.Stdout, "Header: %v\n", w.Header())
}

func main() {
	http.ListenAndServe(":8080", &handler{})
}

为了检查代码,我在命令行中使用了curl。下面是curl命令及其结果:

C:\Users>curl -i -X GET localhost:8080
HTTP/1.1 200 OK
Date: Fri, 19 Aug 2022 03:47:21 GMT
Content-Length: 38
Content-Type: text/plain; charset=utf-8

Method: GET
URL: /
Protocol: HTTP/1.1

由于http.ResponseWriter只是一个接口,并且我没有在程序中实现任何具体类型来满足这个接口,为什么我的代码能够工作?据我所了解,接口方法没有实现。那么我如何从curl获得正确的响应?

另外,正如你在curl的输出中看到的,我收到了一个带有头部的http响应。然而,fmt.Fprintf(os.Stdout, "Header: %v\n", w.Header())的输出是Header: map[]。如果我没错的话,http.ResponseWriter的Header方法应该返回响应头部,根据curl的输出,它不是空的。那么为什么Fprintf返回一个空的map?

谢谢你的帮助。

英文:

I have two questions about the ResponseWriter interface in net/http package. Here is my code

package main

import (
	"fmt"
	"net/http"
	"os"
)

type handler struct{}

func (h *handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	fmt.Fprintf(w, "Method: %v\n", req.Method)
	fmt.Fprintf(w, "URL: %v\n", req.URL)
	fmt.Fprintf(w, "Protocol: %v\n", req.Proto)

	fmt.Fprintf(os.Stdout, "Header: %v\n", w.Header())
}

func main() {
	http.ListenAndServe(":8080", &handler{})
}

and to check the code I used curl in cmd. Below you can see the curl command and its result.

C:\Users>curl -i -X GET localhost:8080
HTTP/1.1 200 OK
Date: Fri, 19 Aug 2022 03:47:21 GMT
Content-Length: 38
Content-Type: text/plain; charset=utf-8

Method: GET
URL: /
Protocol: HTTP/1.1

Since http.ResponseWriter is only an interface and I have not implemented any concrete type in my program to satisfy this interface, why is my code working? As far as I understood there is no implementation for interface methods. So how do I get a correct response from curl?

Also, as you can see in the output of curl, I am receiving an http response with a header. However, output of fmt.Fprintf(os.Stdout, "Header: %v\n", w.Header()) is Header: map[]. If I am right, Header method for http.ResponseWriter must return the response header which is not empty based on the curl output. So why does Fprintf return an empty map?

Thank you for your help.

答案1

得分: 2

由于http.ResponseWriter只是一个接口,并且我在程序中没有实现任何具体类型来满足这个接口,所以为什么我的代码能够工作?

你没有实现,但标准库实现了(具体来说是私有的http.response和http.http2responsewriter类型)。http服务器会将这些类型的实例传递给你的处理程序。你不需要知道它是什么,你只需要知道无论你得到的是什么,它都会实现ResponseWriter方法。

那么为什么Fprintf返回一个空的map呢?

因为这个map是空的,你没有设置任何头部信息。一些服务器(比如Date和Content-Type)如果你没有自己设置,会自动设置这些头部信息,但是这是在你打印之后发生的。

英文:

> Since http.ResponseWriter is only an interface and I have not implemented any concrete type in my program to satisfy this interface, why is my code working?

You haven't; the standard library has (namely, the private http.response and http.http2responsewriter types). The http server passes an instance of one of those types in to your handler. You don't need to know what it is; you just know that whatever you get, it will implement the ResponseWriter methods.

> So why does Fprintf return an empty map?

Because the map is empty, because you didn't set any headers. Some servers (like Date and Content-Type) can be set automatically by the server if you don't set them yourself, but this is happening after the point where you are printing.

huangapple
  • 本文由 发表于 2022年8月19日 12:02:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/73411703.html
匿名

发表评论

匿名网友

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

确定