1 回答

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