Home  >  Article  >  Backend Development  >  What are the golang flow control statements?

What are the golang flow control statements?

青灯夜游
青灯夜游Original
2022-12-28 17:58:284174browse

Flow control statement: 1. if statement, consisting of a Boolean expression followed by one or more statements; 2. "if...else" statement, the expression in else is false in the Boolean expression executed; 3. switch statement, used to perform different actions based on different conditions; 4. select statement; 5. for loop statement, syntax "for k,v := range oldmap{newmap[k]=v}"; 6. Loop control statements break, continue, goto.

What are the golang flow control statements?

The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.

Let’s take a look at the basic content of golang flow control statements.

1. Conditional branch statement

is similar to the C language. The relevant conditional statements are as shown in the following table:

Statement Description
if statement if statement consists of a Boolean expression Followed by one or more statements.
if…else statement The optional else statement can be used after the if statement. The expression in the else statement is executed when the Boolean expression is false.
switch statement The switch statement is used to perform different actions based on different conditions.
select statement The select statement is similar to the switch statement, but select will randomly execute a runnable case. If there is no case to run, it will block until there is a case to run.
  • if statement
    The syntax is as follows:
if 布尔表达式 {
   /* 在布尔表达式为 true 时执行 */
}
  • if-else statement
if 布尔表达式 {
   /* 在布尔表达式为 true 时执行 */
} else {
	/* 在布尔表达式为 false 时执行 */
}
  • switch statement
    The variable <span class="hljs-attribute">v</span> can be of any type, val1 and val2 can be the same Any value of type, the type is not limited to constants or integers, or the final result is an expression of the same type.
switch v {
    case val1:
        ...
    case val2:
        ...
    default:
        ...
}
  • select statement
    select is a control structure in Go, similar to the switch statement used for communication. Each case must be a communication operation, either a send or a receive. It will randomly execute a runnable case. If there is no case to run, it will block until there is a case to run. A default clause should always be runnable.
select {
    case communication clause  :
       statement(s);      
    case communication clause  :
       statement(s);
    /* 你可以定义任意数量的 case */
    default : /* 可选 */
       statement(s);
}

Note:

  • Each case must be a communication
  • All channel expressions will be evaluated and all sent The expressions will be evaluated
  • If any one of the communications can be executed, it will be executed, and the others will be ignored
  • If there are multiple cases that can be executed, select will randomly select one. implement.
  • If no case can be run: If there is a default clause, the default clause will be executed, and the select will be blocked until a certain communication can run, thus avoiding the starvation problem.

2. Loop statement

2.1 Loop processing statement

Different from most languages, the loop statement in Go language only supports the for keyword and does not support the while and do-while structures. The basic usage of the keyword for is very close to that in C language and C .

For is used to implement loops in Go. There are three forms:


Syntax
is the same as for in c language for init; condition; post {}
is the same as for in c language The while is the same as for condition{}
and <span class="hljs-function"><span class="hljs-title">for</span><span class="hljs-params">(;;) in c language </span></span>Same for{}

In addition, the for loop can also be used directlyrangeIterate over slices, maps, arrays and strings, etc., the format is as follows:

for key, value := range oldmap {
	newmap[key] = value
}

##2.2 Loop control statement

Control statementDetailed explanationbreakInterrupt out of the loop or switch statementcontinueSkip the remaining statements of the current loop, and then continue with the next round of loopsgoto statementwill Control transfers to the marked statement

1、break

break主要用于循环语句跳出循环,和c语言中的使用方式是相同的。且在多重循环的时候还可以使用label标出想要break的循环。
实例代码如下:

a := 0
for a<5 {
	fmt.Printf("%d\n", a)
	a++
	if a==2 {
		break;
	}
}
/* output
0
1
2
*/

2、continue

Go 语言的 continue 语句 有点像 break 语句。但是 continue 不是跳出循环,而是跳过当前循环执行下一次循环语句。在多重循环中,可以用标号 label 标出想 continue 的循环。
实例代码如下:

    // 不使用标记
    fmt.Println("---- continue ---- ")
    for i := 1; i <= 3; i++ {
        fmt.Printf("i: %d\n", i)
            for i2 := 11; i2 <= 13; i2++ {
                fmt.Printf("i2: %d\n", i2)
                continue
            }
    }

/* output
i: 1
i2: 11
i2: 12
i2: 13
i: 2
i2: 11
i2: 12
i2: 13
i: 3
i2: 11
i2: 12
i2: 13
*/

    // 使用标记
    fmt.Println("---- continue label ----")
    re:
        for i := 1; i <= 3; i++ {
            fmt.Printf("i: %d", i)
                for i2 := 11; i2 <= 13; i2++ {
                    fmt.Printf("i2: %d\n", i2)
                    continue re
                }
        }

/* output
i: 1
i2: 11
i: 2
i2: 11
i: 3
i2: 11
*/

3、goto

goto语句主要是无条件转移到过程中指定的行。goto语句通常和条件语句配合使用,可用来实现条件转移、构成循环以及跳出循环体等功能。但是并不主张使用goto语句,以免造成程序流程混乱。
示例代码如下:

var a int = 0
LOOP: for a<5 {
	if a == 2 {
		a = a+1
		goto LOOP
	}
	fmt.Printf("%d\n", a)
	a++
}

/*
output:
0
1
2
3
4
*/

以上代码中的LOOP就是一个标签,当运行到goto语句的时候,此时执行流就会跳转到LOOP标志的哪一行上。

【相关推荐:Go视频教程编程教学

The above is the detailed content of What are the golang flow control statements?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn