如何避免在围棋中重复自己



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

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

/*
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
}

相关内容

  • 没有找到相关文章

最新更新