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

tour: Syntax error when the else statement is misplaced #442

Open
valaparthvi opened this issue Mar 25, 2018 · 5 comments · May be fixed by golang/website#66 or golang/website#225
Open

tour: Syntax error when the else statement is misplaced #442

valaparthvi opened this issue Mar 25, 2018 · 5 comments · May be fixed by golang/website#66 or golang/website#225
Labels
NeedsFix The path to resolution is known, but the work has not been done.

Comments

@valaparthvi
Copy link

Context: https://tour.golang.org/flowcontrol/7
The "}" closing brace of if statement must be immediately followed by an else statement if any. Writing else on a separate line(like we do in Python, C, Java, etc) will generate a SyntaxError in Go.
Example:

package main

import (
	"fmt"
	"math"
)

func pow(x, n, lim float64) float64 {
	if v := math.Pow(x, n); v < lim {
		return v
	}
	else {                                                 // misplaced "else" statement
		fmt.Printf("%g >= %g\n", v, lim)
	}
	// can't use v here, though
	return lim
}

func main() {
	fmt.Println(
		pow(3, 2, 10),
		pow(3, 3, 20),
	)
}

Output: prog.go:12:2: syntax error: unexpected else, expecting }

@WheeskyJack
Copy link

Hi,
Above behavior is expected one. "else" statement should be in same line as the closing brace of corresponding "if". Similarly, opening brace of if should be in same line as "if". Otherwise running such code should generate the error. However, opening brace of else can be in the new line. Its always good idea to run go fmt on the code before running the code to format the code as per the go standards and to catch any possible syntax errors.
BR.

@valaparthvi
Copy link
Author

I understand that it's the expected behavior. But what I meant was, it would be helpful if that was mentioned in the tutorial.

@ALTree
Copy link
Member

ALTree commented Jul 4, 2018

Thanks for the report.

In general, I think that slide could use a little more text. It's the one that introduces the else block and it says almost nothing about it.

@ALTree ALTree added the NeedsFix The path to resolution is known, but the work has not been done. label Jul 4, 2018
JackMead pushed a commit to JackMead/tour that referenced this issue Oct 10, 2018
@JackMead
Copy link

Newb trying Hacktoberfest, tiny change but appreciate pointers nonetheless!

sagar23sj added a commit to sagar23sj/website that referenced this issue Jun 15, 2021
Existing description does not contain any description related to else statement.
New changes describes syntax for else statement.

Fixes: golang/tour#442
@Ekasa02
Copy link

Ekasa02 commented Oct 7, 2022

image
bagaimana cara agar output sesuai ketentuan dalam README

crisman added a commit to crisman/golang_website that referenced this issue May 13, 2023
Many users of the Go Tour trip over using else after a newline which
generates a syntax error that is hard to understand unless the reader is
primed to think about a newline before the else. "tour/flowcontrol/7"
("If and else") the slide that first talks about else seems the right place
to update.

Add statement about required location of else after if block, provide an
invalid else code example mirroring the slide's program, and provide links
to the language spec for details.

Fixes golang/tour#1481
Fixes golang/tour#1427
Fixes golang/tour#1062
Fixes golang/tour#442
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
5 participants