Skip to content

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

Open
@valaparthvi

Description

@valaparthvi

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 }

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions