Essential Go  Edit on GitHub      File Issue

if, switch, goto

Basics of if:

	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")
a is not equal to b

Basics of switch:

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")
	fmt.Printf("stmt is '%s'\n", stmt)
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.

  ↑ ↓ to navigate     ↵ to select     Esc to close