(&cli.App{}).Run(os.Args)
- why first ()
are needed?
#1585
-
I came here to ask for (&cli.App{}).Run(os.Args) I couldn't resist asking the question. Why first parens &cli.App{}.Run(os.Args) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Line 308 in c023d9b notice it has the so to correctly
The line you're asking about is doing this exact thing:
the parentheses are there to help with operation ordering. Going from left to right, depth first:
Now, to explain your question bit more: you can't have:
because the order is not correct. What will really happen is this:
so we'd create an object of type
this also won't work because again, we're feeding into you could split this operation into two steps (and in fact that what happens later in the link you've posted), but you still need the address:
notice we first create an object Last example:
Try explaining why the
another important part of this whole thing is the syntax sugar around OOP calls, but in short:
are technically equivalent. 2 won't work in Go, but it will in similar languages like Python, JS or C++. The details here are less important than the pointer magic explained above, but it's worth having in head that the following are pretty much always the same in most general purpose programming languages:
|
Beta Was this translation helpful? Give feedback.
Run()
can be called only on a pointer toApp
structcli/app.go
Line 308 in c023d9b
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
Run()
's definition, you need toApp
.The line you're asking about is doing this exact thing:
the parentheses are there to help with operation ordering.
Going from left to right, depth first: