if, switch, goto

suggest change

Basics of if:

main.go

	a := 5
	b := 6
	if a == b {
		fmt.Print("a is equal to b\n")
	} else {
		fmt.Print("a is not equal to b\n")
	}
}

Output:

a is not equal to b

Basics of switch:

main.go

	stmt := "if"
	switch stmt {
	case "if", "for":
		fmt.Printf("stmt ('%s') is either 'if' or 'for'\n", stmt)
	case "else":
		fmt.Printf("stmt is 'else'\n")
	default:
		fmt.Printf("stmt is '%s'\n", stmt)
	}

Output:

stmt ('if') is either 'if' or 'for'

Notice that unlike in C++, case statement doesn’t fall throught to the next case, so you don’t have to put break at the end of each case.

Switch is also used as a type switch.

if statement

switch statement

goto statements

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents