Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

_content/tour: document no newline before else #225

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions _content/tour/flowcontrol.article
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down