• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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

 
Sheriff
Posts: 17687
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 37
VI Editor Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Junilu Lacar
Sheriff
Posts: 17687
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 17687
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 100
VI Editor Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Junilu Lacar
Sheriff
Posts: 17687
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 100
VI Editor Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
He's giving us the slip! Quick! Grab this tiny ad!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic