Assignment mismatch: 1 variable but mollie.NewClient returns 2 value

238 Views Asked by At

Hi I try to make a test payment with a test key of Mollie in Go

This the code based on their 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)
}

Everything is fine, but when I run the "go run" command I get this error

# 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

I tried to fix it by adding err to this line

mollieClient := mollie.NewClient(apiKey, client) by making it mollieClient, err := mollie.NewClient(apiKey, client)

But it makes it worse because the error will be assignment mismatch: 2 variables but mollie.NewClient returns 3 values

1

There are 1 best solutions below

1
PRATHEESH PC On

You are not using the methods properly. That's the reason you are having these issues.

See the NewClient function syntax

// 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) { 
    ....
}

It is expecting 2 arguments as type *http.Client and *Config, but you provided the first argument as string. This function is returning 2 values, thus we have to use 2 variables

So you have to use

config := mollie.NewConfig(true, mollie.OrgTokenEnv)
mollieClient, err := mollie.NewClient(client, config)

assignment mismatch: 1 variable but mollie.NewClient returns 2 values will be resolved with this change.


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

mollieClient.Payments.Create expecting 3 inputs and returning 3 values.

See the doc for more.

Note : Read through the package, see the function/method syntax, also check the _test.go files to see how they are working with the functionalities. Happy coding :).