For loop
for
loop is the only looping construct in Go.
1 | func main() { |
If
1 | func sqrt(x float64) string { |
Switch
Go’s switch is like the one in C, C++, Java, JavaScript, and PHP, except that Go only runs the selected case, not all the cases that follow. In effect, the break
statement that is needed at the end of each case in those languages is provided automatically in Go. Another important difference is that Go’s switch cases need not be constants, and the values involved need not be integers.
Switch cases evaluate cases from top to bottom, stopping when a case succeeds.
Switch without a condition is the same as switch true
.
1 | import ( |
Defer
A defer statement defers the execution of a function until the surrounding function returns.
The deferred call’s arguments are evaluated immediately, but the function call is not executed until the surrounding function returns.
1 | func main() { |
Deferred function calls are pushed onto a stack. When a function returns, its deferred calls are executed in last-in-first-out order.
1 | func main() { |