if, switch, goto:
if, switch, goto
suggest changeBasics 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")
default:
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.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents
4
Strings
5
Pointers
6
Arrays
7
Slices
8
Maps
9
Structs
10
Interfaces
12
if, switch, goto
15
Functions
16
Methods
18
Defer
20
Concurrency
22
Mutex
23
Packages
27
Logging
30
JSON
31
XML
32
CSV
33
YAML
34
SQL
35
HTTP Client
36
HTTP Server
38
Reflection
39
Context
40
Package fmt
41
OS Signals
42
Testing
49
gob
50
Plugin
53
Console I/O
54
Cryptography
59
Contributors