How do you read this function syntax?

I have see this:

func (c *Command) AddCommand(cmds ...*Command) {

but what does this mean?
I assume:

  • function name AddCommand
  • Input Parameters: ?
  • Return type: ?

Same question for:

func (c *Command) Execute() error {
  1. c* Comand is a pointer to Command type and it is pointer receiver. That means you can call ths function like this
    c = &Command{}
    c.AddCommand(…)

  2. cmds… *Comabds means that this function receives any number of Command pointer parameters.
    it is called variadic function
    For example
    var c1, c2, c2 *Command
    c1 := &Command{}
    c2 = &Command{}
    c3 = new(Command)
    c1.AddCommand(c2,c3)
    – or –
    c1.AddCommand(c2)
    – or –
    c1.AddCommand(c1, c2, c3)

  3. There is no return type in this function

In your last question, the function return an error and use a *Command as a receiver.

HTH
Yamil

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