[GO column-6] Go language flow control

[GO column-6] [Go language] flow control

Process control

Flow control is the greatest invention in programming languages, because with it, you can express very complex logic through very simple flow description.

if statement

ifPerhaps the most common of all programming languages, its syntax can be summed up as:

Do something if the condition is met, otherwise do another.

if x > 10 {
    fmt.Println("x is greater than 10")
} else {
    fmt.Println("x is less than 10")

Another powerful feature of Go’s if is that a variable is allowed to be declared in the conditional judgment statement. The scope of this variable can only be within the conditional logic block, and other places will not work as follows:

// Calculate the value x, and then judge whether it is greater than 10 according to the size returned by x. 
if x := computedValue(); x > 10 {
    fmt.Println("x is greater than 10")
} else {
    fmt.Println("x is less than 10")

//If this place is called like this, the compilation will go wrong, because x is the variable in the condition 

When multiple conditions are as follows:

if count == 3 {
    fmt.Println("The count is equal to 3")
} else if count < 3 {
    fmt.Println("The count is less than 3")
} else {
    fmt.Println("The count is greater than 3")


Go has the goto statement – use it wisely. Use goto to jump to a label that must be defined within the current function.

For example, consider a loop like this:

func myFunc() {
i := 0
Here: //The first word of this line, ending with a colon as a label
goto Here //Go to Here }

Tag names are case sensitive.

for statement

One of the most powerful control logic in Go is for, which can be used to read data in a loop, and can be used as a while to control logic and iterative operations. Its syntax is as follows:

for expression1; expression2; expression3 {

expression1, expression2and expression3are expressions, where expression1and expression3are variable declarations or return values ​​of function calls, etc., which expression2are used for conditional judgment, and expression1are called before the start of the loop and expression3at the end of each round of the loop.

An example is more useful than the above, so let’s take a look at the following example:

package main
import "fmt"
func main(){
    sum := 0;
    for index:=0; index < 10 ; index++ {
    sum += index
    fmt.Println("sum is equal to ", sum)
// output: sum is equal to 45

Sometimes you need to perform multiple assignment operations. Since there is no , operation in Go, you can use parallel assignment i, j = i+1, j-1. Sometimes if we ignore expression1and expression3, as follows:

sum := 1
for ; sum < 1000; {
    sum += sum

Among them; can also be omitted, then it becomes the following code, is it familiar?

Yes, this is the function of while.

sum := 1
for sum < 1000 {
    sum += sum

There are two key operations in the loop breakand continue, the breakoperation is to jump out of the current loop and continueskip this loop. When the nesting is too deep, it breakcan be used with the label, that is, jump to the position specified by the label. For details, refer to the following example:

for  index := 10 ; index > 0 ; index -- {
 if  index == 5 {
     break // or continue
// break prints 10 , 9 , 8 , 7 , 6 
// continue prints 10 , 9 , 8 , 7 , 6 , 4 , 3 , 2 , 1

breakAnd continuecan also be followed by a label to jump to the outer loop in multiple loops, and can befor used to read the data of and :range

for k,v:=range map {
    fmt.Println("map's key:",k)
    fmt.Println("map's val:",v)

for _, v := range map{
    fmt.Println("map's val:", v)


Sometimes you need to write a lot if-elseto implement some logic processing. At this time, the code looks ugly and verbose, and it is not easy to maintain in the future. At this time, switchthis problem can be solved very well. Its syntax is as follows:

switch sExpr {
    case expr1:
        some instructions
    case expr2:
        some other instructions
    case expr3:
        some other instructions
        other code

sExprand expr1, expr2, expr3and must be of the same type. Go’s switchis so flexible that expressions don’t have to be constants or integers and the process is executed from top to bottom until a match is found; instead, if there is switchno expression, it will match true.

i := 10
switch i {
case 1:
    fmt.Println("i is equal to 1")
case 2, 3, 4:
    fmt.Println("i is equal to 2, 3 or 4")
case 10:
    fmt.Println("i is equal to 10")
    fmt.Println("All I know is that i is an integer")

In the above code, we aggregated a lot of values ​​into one caseinside , at the same time, the inside is equivalent to each last with by defaultGo , after a successful match, it will not automatically execute the other , but jump out of the whole , but you can use to force the execution of the following code .switch

integer := 6
switch integer {
case 4:
    fmt.Println("The integer was <= 4")
case 5:
    fmt.Println("The integer was <= 5")
case 6:
    fmt.Println("The integer was <= 6")
case 7:
    fmt.Println("The integer was <= 7")
case 8:
    fmt.Println("The integer was <= 8")
    fmt.Println("default case")

The above program will output:

The integer was <= 6
The integer was <= 7
The integer was <= 8
default case

💫Click on the direct data to receive💫

​There are all kinds of learning materials, interesting and fun programming projects, and various resources that are hard to find. ​ ​

❤️Follow Suzhou program Dabai public account ❤️

👇 👇👇

Leave a Comment

Your email address will not be published. Required fields are marked *