03. Flow Control
if
условие
if
условиеУсловное выражение if
в Go:
Like for
, the if
statement can start with a short statement to execute before the condition.
Variables declared by the statement are only in scope until the end of the
if
.Variables declared inside an
if
short statement are also available inside any of the else blocks.
for
loop
for
loopGo has only one looping construct, the for
loop. The basic for
loop has three components separated by semicolons:
the init statement: executed before the first iteration
the condition expression: evaluated before every iteration
the post statement: executed at the end of every iteration
Классический цикл по массиву или слайсу:
Note: Unlike other languages like C, Java, or JavaScript there are no parentheses surrounding the three components of the for
statement and the braces { }
are always required.
The init and post statement are optional. At that point you can drop the semicolons:
If you omit the loop condition it loops forever, so an infinite loop is compactly expressed.
Range for
loop
for
loopThe
range
form of thefor
loop iterates over a slice, map or channel.When ranging over a slice, two values are returned for each iteration. The first is the index, and the second is a copy of the element at that index.
The
range
loop evaluates the provided expression only once, before the beginning of the loop, by doing a copy (regardless of the type).When a range loop iterates over a data structure, it performs a copy of each element to the value variable (the second item). To avoid copy either use a classic for loop or a range loop using the index instead of the value variable.
Iterating over using the range loop, regardless of the number of elements, creates a single variable for the value with a fixed address. This value is being reassigned on each iteration, but it keeps the same address.
You can skip the index or value by assigning to _
.
It’s important to know that range
is making a copy of the value, not returning a reference. If you use the address of the value variable as a pointer to each element, you’ll be making a mistake.
Iterating over map you shouldn’t make any ordering assumptions at all:
It doesn’t keep the data sorted by key (a map isn’t based on a binary tree).
It doesn’t preserve the order in which the data was added.
Randomize order for each iteration.
An element being produced during the same iteration in which it’s added.
switch
switch
A switch
statement is a shorter way to write a sequence of if - else
statements. It runs the first case whose value is equal to the condition expression.
Go only runs the selected case, not all the cases that follow.
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.
the order of the
default
branch in aswitch-case
control flow block can be arbitrarySwitch without a condition is the same as switch
true
.break
inside case interrupt current casefallthrough
keyword used to indicate that next case should be proceed.
Case could contain multiple options:
Blank switch: If the switch has no expression it switches on true. It's therefore possible and idiomatic—to write an if-else-if-else
chain as a switch.
Favor blank switch statements over if/else chains when you have multiple related cases. Using a switch makes the comparisons more visible and reinforces that they are a related set of concerns.
Labels and goto
goto
A
goto
keyword must be followed by a label to form a statement.A label which name is not the blank identifier must be used at least once.
A
goto
statement will make the execution jump to the next statement following the declaration of the label used in thegoto
statement.if a label is declared within the scope of a variable, then the uses of the label can't appear before the declaration of the variable.
Метка может находится и ниже, но переход по ней не должен означать вводить новых переменых (что является side effect-ом).
Last updated
Was this helpful?