Skip to content

(&cli.App{}).Run(os.Args) - why first () are needed? #1585

Answered by arturtamborski
abitrolly asked this question in Q&A
Discussion options

You must be logged in to vote

Run() can be called only on a pointer to App struct

cli/app.go

Line 308 in c023d9b

func (a *App) Run(arguments []string) (err error) {

notice it has the * (asterisk) character, meaning it takes a pointer. Pointer values are addresses, and you get these addresses by prefixing your values with the & character.

so to correctly

  1. call the Run()'s definition, you need to
  2. feed it a pointer (an address)
  3. to a value of type App.

The line you're asking about is doing this exact thing:

(&cli.App{}).Run(os.Args)
  ^^^^^^^^^ - 3
 ^ - 2
             ^^^^^^^^^^^^ - 1

the parentheses are there to help with operation ordering.

Going from left to right, depth first:

  • create a object of t…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@abitrolly
Comment options

abitrolly Jul 20, 2023
Maintainer Author

Answer selected by abitrolly
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants