diff --git a/_content/tour/flowcontrol.article b/_content/tour/flowcontrol.article index 448cb76765..2c64e0a72a 100644 --- a/_content/tour/flowcontrol.article +++ b/_content/tour/flowcontrol.article @@ -69,6 +69,20 @@ of the `else` blocks. (Both calls to `pow` return their results before the call to `fmt.Println` in `main` begins.) +Because of the way Go parses source code you must put the `else` on the same line +as the closing brace of the associated `if` block. +`else` after a newline generates a syntax error: + + if v := math.Pow(x, n); v < lim { + return v + } + else { // syntax error: unexpected else, expecting } + fmt.Printf("%g >= %g\n", v, lim) + } + +If you are interested in details on why this is, read the Language Specification sections +on [[/ref/spec#Semicolons][Semicolons]] and [[/ref/spec#If_statements][If Statements]]. + .play flowcontrol/if-and-else.go * Exercise: Loops and Functions