亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在服務HTTP的http中傳遞的原始類型。響應編寫器接口?

在服務HTTP的http中傳遞的原始類型。響應編寫器接口?

Go
慕沐林林 2022-10-04 15:52:27
在學習期間,我實際上想知道如何回到聽眾身邊。從這個答案中,我發現了http。響應類型被傳遞給ServeHTTP,但事實并非如此,因為在編譯時下面的代碼拋出錯誤。這意味著不實現接口。我很好奇實現接口的類型是什么?net/httphttp.Responsehttp.Responsehttp.ResponseWriterhttp.ResponseWriter# command-line-arguments./server1.go:9:20: http.Response.Header is a field, not a method./server1.go:9:20: impossible type assertion:        http.Response does not implement http.ResponseWriter (missing Header method)func handler(resp http.ResponseWriter, req *http.Request){    actualresp := resp.(http.Response) //https://tour.golang.org/methods/15    resp.Write([]byte("Hello Web, from go"))}func main(){    http.HandleFunc("/api", handler)    http.ListenAndServe(":8000", nil)}
查看完整描述

1 回答

?
慕絲7291255

TA貢獻1859條經驗 獲得超6個贊

網易娛樂.響應僅具有以下方法:

func (r *Response) Cookies() []*Cookie

func (r *Response) Location() (*url.URL, error)

func (r *Response) ProtoAtLeast(major, minor int) bool

func (r *Response) Write(w io.Writer) error

這不會實現 http。響應編寫器接口,需要:

Header() Header

Write([]byte) (int, error)

WriteHeader(statusCode int)

所以顯然不能用作.http.Responsehttp.ResponseWriter


相反,正如您鏈接到的答案所提到的,將使用未導出的 http.響應。您可以在代碼中找到對 http 服務器端寫入的生存期的描述。


查看完整回答
反對 回復 2022-10-04
  • 1 回答
  • 0 關注
  • 89 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號