03. Flow Control

if условие

Условное выражение if в Go:

func sqrt(x float64) string {
    if x < 0 {
        return sqrt(-x) + "i"
    }
    return fmt.Sprint(math.Sqrt(x))
}

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.

func pow(x, n, lim float64) float64 {
    if v := math.Pow(x, n); v < lim {
        return v
    } else {
        fmt.Printf("%g >= %g\n", v, lim)
    }
    return lim
}

for loop

Go 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

  • The range form of the for 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

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 a switch-case control flow block can be arbitrary

  • Switch without a condition is the same as switch true.

  • break inside case interrupt current case

  • fallthrough 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

  • 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 the goto 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?