This is a creation in Article, where the information may have evolved or changed.
Conditional statements
If ... else statement, such as:
if - { FMT. Println (">100") Elseif0 < num { FMT. Println ("<0"Else { fmt. Println ("0<<num<<100")}
Looping statements
Switch statements in the go language are divided into two, one is the expression switch statement, and the other language switch uses the same method, the other is the type to determine the switch statement, which is similar to the type assertion, but use the Type keyword to act as the type being judged.
An example of an expression switch is as follows:
//An expression switch SwitchContent: =getcontent (); content {default: FMT. Println ("Unknow Language") Case "Lua": Break Case "Python": FMT. Println ("python") Case "C","C + +","Java": FMT. Println ("A Compiled language") }
Examples of type-judging switch statements are:
V: ="3" Switch Interface{} (v). (type) { Case string: Log. Printf ("Thie string is '%s '. \ n", V) Case int,UINT, int8, Uint8, Int16, Uint16:log. Printf ("Thie integer is%d.\n", V)default: Log. Printf ("unsupported value. (type=%t) \ n", v)}
In addition, the switch statement can implement an alternative to a series if statement, which makes the code appear clearer and easier to read, and in the absence of a switch expression, the switch determines that the target is treated as a Boolean type, and the first case expression that returns true will be executed, such as:
switch { case num > 100 : Log. Println ( >100 " " case num < 0 : Log. Println ( <0 " " default : Log. Println ( 0<<num<<100 )}
There are three usages of the for statement, the first is the general usage, the structure is the initialization clause, the condition, the post clause, such as:
0 for 0 ; i++ { + = I }
The second is similar to the function of the while in other languages, noting that there is no while loop in the go language, such as:
0 for - { 2 }
The third is similar to the role of foreach in other languages, used to iterate over the types of algebraic groups and dictionaries, such as:
M: = map[string]int{"A"1"B" 2 } for K, _: = range m { log. Print (k) }
Note: The If/for/switch statement can accept an optional initialization clause; The Break\continue\goto statement can jump to the specified tag, and the definition of the tag uses the form "identifier:".
Goto statement
A goto statement can only be executed with markup to jump to a specified location, which is controversial in other languages and is generally not recommended for code readability.
Defer statements
A Process Control statement that is unique to the go language, which is used to schedule a call to a function. It can only appear in a function (assuming a function), and can only invoke another function (assuming that it is a B function), meaning that at the end of a function return, deferred call B function, generally used to open the file when the resource cleanup work. If more than one defer statement is called inside a function, the principle of last-in-first-out is followed. Defer statements can be followed by anonymous functions to quickly implement some temporary functions. The variables that can be used by the function called by the defer can be passed in by parameters, or they can be called in the context, and if they are passed in, they are evaluated immediately, and if they are variables in context, they are not evaluated immediately, but are taken at the value of the defer function call.
Exception Handling Statements
Switch statement