gpt4 book ai didi

go - 如何避免在 Go 中重复自己?

转载 作者:行者123 更新时间:2023-12-01 22:43:20 25 4
gpt4 key购买 nike

我刚开始使用 Go,我的背景包括泛型。由于 Go 仍然不支持泛型,我想知道如何保持我的代码 DRY?

看看下面的例子,request参数有一个动态类型,它返回一个动态响应(PaymentMethodResponse)。如果我想创建另一个请求,我将整个代码复制并粘贴到方法中,只更改 request 的类型。和 responselocalVarPath多变的。

/*
PaymentMethods Returns available payment methods.
Queries the available payment methods for a transaction based on the transaction context (like amount, country, and currency). Besides giving back a list of the available payment methods, the response also returns which input details you need to collect from the shopper (to be submitted to `/payments`). Although we highly recommend using this endpoint to ensure you are always offering the most up-to-date list of payment methods, its usage is optional. You can, for example, also cache the `/paymentMethods` response and update it once a week.
* @param request PaymentMethodsRequest - reference of PaymentMethodsRequest).
* @param ctxs ..._context.Context - optional, for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
@return PaymentMethodsResponse
*/

func (a *Checkout) PaymentMethods(request *PaymentMethodsRequest, ctxs ..._context.Context) (PaymentMethodsResponse, *_nethttp.Response, error) {
var (
localVarHTTPMethod = _nethttp.MethodPost
localVarPostBody interface{}
localVarReturnValue PaymentMethodsResponse
)

// create path and map variables
localVarPath := a.BasePath() + "/paymentMethods"
localVarHeaderParams := make(map[string]string)
localVarQueryParams := _neturl.Values{}

// to determine the Content-Type header
localVarHTTPContentTypes := []string{"application/json"}

// set Content-Type header
localVarHTTPContentType := common.SelectHeaderContentType(localVarHTTPContentTypes)
if localVarHTTPContentType != "" {
localVarHeaderParams["Content-Type"] = localVarHTTPContentType
}

// to determine the Accept header
localVarHTTPHeaderAccepts := []string{"application/json"}

// set Accept header
localVarHTTPHeaderAccept := common.SelectHeaderAccept(localVarHTTPHeaderAccepts)
if localVarHTTPHeaderAccept != "" {
localVarHeaderParams["Accept"] = localVarHTTPHeaderAccept
}
// body params
if request != nil {
localVarPostBody = request
}

var ctx _context.Context
if len(ctxs) == 1 {
ctx = ctxs[0]
}

r, err := a.Client.PrepareRequest(ctx, localVarPath, localVarHTTPMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams)
if err != nil {
return localVarReturnValue, nil, err
}

localVarHTTPResponse, err := a.Client.CallAPI(r)
if err != nil || localVarHTTPResponse == nil {
return localVarReturnValue, localVarHTTPResponse, err
}

localVarBody, err := _ioutil.ReadAll(localVarHTTPResponse.Body)
localVarHTTPResponse.Body.Close()
if err != nil {
return localVarReturnValue, localVarHTTPResponse, err
}

if localVarHTTPResponse.StatusCode >= 300 {
newErr := common.NewAPIError(localVarBody, localVarHTTPResponse.Status)
return localVarReturnValue, localVarHTTPResponse, newErr
}

err = a.Client.Decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
if err != nil {
newErr := common.NewAPIError(localVarBody, err.Error())
return localVarReturnValue, localVarHTTPResponse, newErr
}

return localVarReturnValue, localVarHTTPResponse, nil
}


用法示例:(请求是 json 结构)

res, httpRes, err := client.Checkout.PaymentMethods(&checkout.PaymentMethodsRequest{})

最佳答案

您可以使用与 json.Unmarshal 使用的方法相同的方法。和其他接受 interface{} 类型参数的解码器/解码器并且不是返回未知类型的值,而是将操作结果存储到提供的 interface{} 中。争论。

这是示例伪代码:

func apicall(req, res interface{}) error {
inputbody, err := jsonencode(req)
if err != nil {
return err
}

response, err := httpclient.postrequest(inputbody)
if err != nil {
return err
}

return jsondecode(res, response.body)
}

func main() {
req := new(PaymentMethodsRequest)
res := new(PaymentMethodsResponse)
if err := apicall(req, res); err != nil {
return err
}

// do something with res
}

关于go - 如何避免在 Go 中重复自己?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61181471/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com