Help with calling a function


(Jared Carey) #1

I’m struggling with calling a function where the arguments are a return from another function.

Working function

cmd := func(apiResp interface{}, err error) { ... }

I can call that function with

cmd(cli.ServerVersion(context.Background()))

where cli.ServerVersion() == func (cli *Client) ServerVersion(ctx context.Context) (types.Version, error). 2 return types going to the cmd function.

Adding a third argument to cmd is where things break.

cmd := func(apiResp interface{}, err error, path string) { ... }

calling that with

cmd(cli.ServerVersion(context.Background()), "the/file/path")

causes the following errors:

  • multiple-value cli.ServerVersion() in single-value context```
    
  • not enough arguments in call to cmd
    

go version go1.11.4 darwin/amd64


(Johan Dahl) #2

Hi

It is a feature of go to be able to call a function with n number of arguments by first invoking a fucntion which returns the same number of arguments (and compatible types). Se this posting: Print string and return values of a function with Println


(Ivan Matmati) #3

Hi,

It’s a case of multiple assigments :
https://gobyexample.com/multiple-return-values
The most generalized example in the part “You can declare multiple variables at once.” :
https://gobyexample.com/variables


(system) closed #4

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.