登录
首页 >  Golang >  Go问答

赋值不匹配:1 个变量,但 mollie.NewClient 返回 2 个值

来源:stackoverflow

时间:2024-04-07 19:57:35 264浏览 收藏

目前golang学习网上已经有很多关于Golang的文章了,自己在初次阅读这些文章中,也见识到了很多学习思路;那么本文《赋值不匹配:1 个变量,但 mollie.NewClient 返回 2 个值》,也希望能帮助到大家,如果阅读完后真的对你学习Golang有帮助,欢迎动动手指,评论留言并分享~

问题内容

您好,我尝试使用 go 中的 mollie 测试密钥进行测试支付

这是基于他们的 github 的代码

"fmt"
    "net/http"
    "github.com/victoravelar/mollie-api-go/v3/mollie"
)

func main() {
    apikey := "test_key"

    client := http.defaultclient

    mollieclient := mollie.newclient(apikey, client)

    payment, err := mollieclient.payments.create(&mollie.payment{
        amount: &mollie.amount{
            currency: "eur",
            value:    "10.00",
        },
        description: "testpayment with go",
        redirecturl: "https://mollie.com",
    })

    if err != nil {
        fmt.println("fout bij het creëren van de betaling:", err)
        return
    }

    fmt.println("betaling aangemaakt. ga naar deze url om de betaling te voltooien:")
    fmt.println(payment.links.checkout.href)
}

一切都很好,但是当我运行“go run”命令时,出现此错误

# command-line-arguments
.\mollie_example.go:14:18: assignment mismatch: 1 variable but mollie.NewClient returns 2 values
.\mollie_example.go:14:35: cannot use apiKey (variable of type string) as *http.Client value in argument to mollie.NewClient        
.\mollie_example.go:14:43: cannot use client (variable of type *http.Client) as *mollie.Config value in argument to mollie.NewClient

我尝试通过将 err 添加到此行来修复它

mollieclient := mollie.newclient(apikey, 客户端) 通过制作它 mollieclient, err := mollie.newclient(apikey, client)

但这会让情况变得更糟,因为错误将是赋值不匹配:2 个变量,但 mollie.newclient 返回 3 个值


正确答案


您没有正确使用这些方法。这就是您遇到这些问题的原因。

查看NewClient函数语法

// newclient returns a new mollie http api client.
// you can pass a previously build http client, if none is provided then
// http.defaultclient will be used.
//
// newclient will lookup the environment for values to assign to the
// api token (`mollie_api_token`) and the organization token (`mollie_org_token`)
// according to the provided config object.
//
// you can also set the token values programmatically by using the client
// withapikey and withorganizationkey functions.
func newclient(baseclient *http.client, conf *config) (mollie *client, err error) { 
    ....
}

它需要 2 个类型为 *http.client*config 的参数,但您以字符串形式提供了第一个参数。 该函数返回 2 个值,因此我们必须使用 2 个变量

所以你必须使用

config := mollie.newconfig(true, mollie.orgtokenenv)
mollieclient, err := mollie.newclient(client, config)

赋值不匹配:1 个变量,但 mollie.newclient 返回 2 个值 将通过此更改得到解决。

func (ps *PaymentsService) Create(ctx context.Context, p Payment, opts *PaymentOptions) (
    res *Response,
    np *Payment,
    err error,
) {
    .....
}

mollieClient.Payments.Create 期望 3 个输入并返回 3 个值。

请拨打 doc 了解更多信息。

注意:通读包,查看函数/方法语法,还检查 _test.go 文件以了解它们如何使用这些功能。快乐编码:)。

文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《赋值不匹配:1 个变量,但 mollie.NewClient 返回 2 个值》文章吧,也可关注golang学习网公众号了解相关技术文章。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>