1 回答

TA貢獻(xiàn)1789條經(jīng)驗(yàn) 獲得超8個(gè)贊
您不必實(shí)現(xiàn)這些功能。 已經(jīng)實(shí)現(xiàn)了 io。作者
:*http.Response
以 HTTP/1.x 服務(wù)器響應(yīng)格式寫入 r 到 w,包括狀態(tài)行、標(biāo)頭、正文和可選的尾部。
package main
import (
"net/http"
"os"
)
func main() {
r := &http.Response{}
r.Write(os.Stdout)
}
在上面的示例中,零值打?。?/p>
HTTP/0.0 000 狀態(tài)代碼 0
內(nèi)容長度: 0
游樂場: https://play.golang.org/p/2AUEAUPCA8j
如果在編寫方法中需要其他業(yè)務(wù)邏輯,則可以嵌入到定義的類型中:*http.Response
type RespWrapper struct {
*http.Response
}
func (w *RespWrapper) toStdOut() {
_, err := io.Copy(os.Stdout, w.Body)
if err != nil {
panic(err)
}
}
但是,您必須使用 構(gòu)造一個(gè)類型的變量:RespWrapper*http.Response
func main() {
// resp with a fake body
r := &http.Response{Body: io.NopCloser(strings.NewReader("foo"))}
// or r, _ := http.Get("example.com")
// construct the wrapper
wrapper := &RespWrapper{Response: r}
wrapper.toStdOut()
}
有沒有辦法使網(wǎng)址。獲取方法吐出自定義類型
不可以,返回類型是 ,這是函數(shù)簽名的一部分,您無法更改它。http.Get
(resp *http.Response, err error)
- 1 回答
- 0 關(guān)注
- 70 瀏覽
添加回答
舉報(bào)