aspose file tools*
The moose likes Other Languages and the fly likes Tour of Go, p21 - if: what is the order of execution? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Languages » Other Languages
Bookmark "Tour of Go, p21 - if: what is the order of execution?" Watch "Tour of Go, p21 - if: what is the order of execution?" New topic
Author

Tour of Go, p21 - if: what is the order of execution?

Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4757
    
    7

I've been going through the "Tour of Go" found at http://tour.golang.org and the example about if on page 21 of the offline version (Edit: it's #22 in the online version) just has me scratching my head. Can't reconcile the output with what I think is the order of execution here.


Output when I run this is listed below. Why did "27 >= 20" get printed first? I expected it to be 9 because of line 5.

To add to my confusion, if I flip the order of the parameters to fmt.Println on lines 15 and 16, the output is

Notice how only 20 and 9 got flipped. I was expecting "9 20" to be the first line in the output, then "27 >= 20" but that didn't happen.
And if I remove the last comma on line 16, the one after the second parameter to fmt.Println, I get a compiler error:

syntax error: unexpected semicolon or newline, expecting

Any help in grokking the order of execution would be greatly appreciated.


Junilu - [How to Ask Questions] [How to Answer Questions]
Mark Summerfield
author
Ranch Hand

Joined: Jun 20, 2012
Posts: 37

Hi Junilu,

My reading is this.
The pow(3, 2, 10) call results in v being assigned 9 which is less than 10 so v (i.e., 9) is returned. Note that v's scope is the if statement in which it is declared (including all else if and else branches). The pow(3, 3, 20) call results in v being assigned 27 which is greater than 20 so the print statement should be executed and lim (i.e., 20) returned. So I'd expect it to print:

The reason that 27 >= 20 prints first is because that print statement occurs inside a pow() call. The fmt.Println() in main cannot execute until it knows what its parameters are, so the two pow() calls must be done first, and in one of them the 27 >= 20 line is printed. Once the pow() calls complete the fmt.Println() in main then prints. Flipping the pow() calls will make no difference (except to whether you get 9 20 or 20 9) since both pow() calls must be done before the main fmt.Println() and one of them outputs the 27 >= 20 line.

So the behavior is exactly as expected.


Mark Summerfield
"Programming in Go" - http://www.qtrac.eu/gobook.html
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4757
    
    7

Thanks Mark,

My brain must have been really addled from lack of sleep -- I went to bed right after when I caught myself talking to myself out loud about how confusing the code was -- usually a sign that my body was screaming out for some rest (that's my excuse and I'm sticking to it!). Now, in the light of the morning and your explanation, it makes perfect sense. For some reason, I was thinking maybe the pow() function was returning two values, v and lim, with one call but couldn't reconcile that with the return type declaration either.

Thanks again for clarify that for me.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4757
    
    7

A couple of other things:

Just went out to http://tour.golang.org and the example above now seems to be #22 instead of #21 -- [EDIT: I see what it is now. I downloaded the tour for offline viewing. The difference is that the offline version doesn't have the #2 page that the online version does, so offline version has all the pages shifted down by one]

Something strange about the fmt.Println() in main(). If you keep the original formatting given in the example, the comma after the second parameter is required; deleting it will give you a compilation error. However, if I reformat the code and put everything in one line, removing that last comma is fine. That's a bit surprising. What gives here?
Igor Mechnikov
Ranch Hand

Joined: Feb 13, 2011
Posts: 100

Junilu Lacar wrote:A couple of other things:

Just went out to http://tour.golang.org and the example above now seems to be #22 instead of #21 -- [EDIT: I see what it is now. I downloaded the tour for offline viewing. The difference is that the offline version doesn't have the #2 page that the online version does, so offline version has all the pages shifted down by one]

Something strange about the fmt.Println() in main(). If you keep the original formatting given in the example, the comma after the second parameter is required; deleting it will give you a compilation error. However, if I reformat the code and put everything in one line, removing that last comma is fine. That's a bit surprising. What gives here?


I found an explanation "Composite literals require trailing commas before newlines because of how semi-colon insertion works: http://golang.org/ref/spec#Semicolons" in this discussion: http://comments.gmane.org/gmane.comp.lang.go.general/64463


String knock = "\u042F \u0418\u0433\u043e\u0440\u044c";
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4757
    
    7

Thanks Igor,

Very interesting to learn about the automatic insertion of semicolons. I may like this "feature" after all when you think about not having to worry about the trailing commas when you move lines around.
Igor Mechnikov
Ranch Hand

Joined: Feb 13, 2011
Posts: 100

Junilu Lacar wrote:Thanks Igor,

Very interesting to learn about the automatic insertion of semicolons. I may like this "feature" after all when you think about not having to worry about the trailing commas when you move lines around.


Exactly right, I saw this very point lauded in the discussion above.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Tour of Go, p21 - if: what is the order of execution?