我想编写一个小的帮助器函数来集中我的HTTP调用。来自Python,我仍然在Go中使用指针。
帮助器函数实质上采用带有调用信息(URL,方法和可选请求正文)的a,并将响应正文返回为(现在是):struct[]bytenil
package main
import "net/http"
type httpParameters struct {
Url string
Method func(client http.Client, string2 string) (*http.Response, error)
Body []byte
}
func callHTTP(param httpParameters) (resp []byte, err error) {
return nil, nil
}
附加的测试是
package main
import (
"net/http"
"testing"
)
func TestCallHTTP(t *testing.T) {
params := httpParameters{
Url: "https://postman-echo.com/get",
Method: http.Client.Get,
}
resp, err := callHTTP(params)
if err != nil {
t.Errorf("call to %v was not successful: %v", params.Url, err)
}
if resp != nil {
t.Errorf("GET call to %v returned something: %v, should be nil", params.Url, resp)
}
}
当试图运行我得到
.\main_test.go:11:22: invalid method expression http.Client.Get (needs pointer receiver: (*http.Client).Get)
注意:在类型声明中是一种试错法 - 我最终将内容放在定义中。我不确定这是应该引用这些功能的方式Method func(client http.Client, string2 string) (*http.Response, error)Get
我应该如何解决该方法以便能够在调用中传递它?Get
江户川乱折腾
慕妹3146593
相关分类