2 回答

TA貢獻1851條經驗 獲得超5個贊
http.NewRequest(method, url, body)將有助于實現同樣的目標。
req, err := http.NewRequest("GET", "http://example.com", nil)
// ...
req.Header.Add("If-None-Match", `W/"wyzzy"`)
resp, err := client.Do(req)
// ...

TA貢獻1820條經驗 獲得超9個贊
Get 是用指針接收器聲明的,即 而不是使用值接收器,即不是 。*http.Clienthttp.Client
錯誤:
invalid method expression http.Client.Get (needs pointer receiver: (*http.Client).Get)
就是這樣說的。而且,它甚至提供了方法表達式的正確形式,即.(*http.Client).Get
這也意味著函數的簽名必須相應地更改,即更改為 。client http.Clientclient *http.Client
type httpParameters struct {
Url string
Method func(*http.Client, string) (*http.Response, error)
Body []byte
}
func TestCallHTTP(t *testing.T) {
params := httpParameters{
Url: "https://postman-echo.com/get",
Method: (*http.Client).Get,
}
// ...
}
https://play.golang.org/p/83qgE4QeHx5
- 2 回答
- 0 關注
- 83 瀏覽
添加回答
舉報