当前位置:网站首页>Go-11 - Process Control
Go-11 - Process Control
2022-08-09 01:32:00 【hahyyy】
Conditional judgment
Go language provides the following conditional judgment statements
Statement | Description |
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 |
if nested statement | You can embed one or more if or else if statements within an if or else if statement |
switch statement | Used to perform different actions based on different conditions |
select statement | Similar to a switch statement, but select will execute a random runnable case If there is no case to run, it will block until there is a case to run |
if else
package mainimport "fmt"func main() {age := 1// The { after the if statement cannot wrapif age <= 18 {fmt.Println("kid")}else{fmt.Println("adult")}}
switch
// defines a new type Gender using the type keywordtype Gender int8// constant => all uppercase identifiersconst (MALE Gender = 1FEMALE Gender = 2)gender := MALE// Match the following case statement value -> match genderswitch gender {case FEMALE:fmt.Println("female")case MALE:fmt.Println("male")default:fmt.Println("unknow")}
Loop
Go language provides the following types of loop processing statements
loop type | Description |
for loop | Repeat block of statements |
Loop Nesting | Nest one or more for loops within a for loop |
Go language supports the following loop control statements
Control Statement | Description |
break statement | often used to interrupt the current for loop or jump out of a switch statement |
continue statement | Skip the remaining statements of the current loop and continue to the next loop |
goto statement | Transfer control to the marked statement |
Infinite loop
If the conditional statement in the loop is never false, an infinite loop will be executed. We can execute an infinite loop by setting only one conditional expression in the for loop statement
for true{// It's ok without true// for {fmt.Println("This is an infinite loop")}
for Accumulation
sum := 0// Here i is a temporary variable, which will be recycled after usefor i := 0; i < 10; i++ {if sum > 50 {break}sum += i}// The usage of break and continue is no different from other languagesfmt.Println(sum)
Traverse
Use for range to traverse arrays, slices, and dictionaries
nums := []int{10, 20, 30, 40}for i, num := range nums {fmt.Println(i, num)}// 0 10// 1 20// 2 30// 3 40m2 := map[string]string{"Sam": "Male","Alice": "Female",}for key, value := range m2 {fmt.Println(key, value)}// Sam Male// Alice Female
边栏推荐
猜你喜欢
随机推荐
momerymap mmap 存储映射I/O
TCP/IP协议栈
LeetCode每日两题02:轮转数组 (均1200道)
typescript90-使用类型文件声明类型
Bugs encountered in remote control projects
ONNX是什么?怎么用?[简明解读版]
睿智的目标检测61——Tensorflow2 Focal loss详解与在YoloV4当中的实现
右键新建缺少word、excel选项问题处理
clickhouse 思维导图
任务五 处理连续型数据
经典卷积神经网络ZFNet--解卷积可视化
MySQL存储过程与函数
[Cellular Automata] Simulation of emergency evacuation of disaster personnel under social force factors based on cellular automata with matlab code attached
4-11 Matplotlib 配置
轻量级网络SqueezeNet学习记录
基于机器学习之模型树短期负荷预测(Matlab代码实现)
[深入研究4G/5G/6G专题-55]: L3信令控制-4-软件功能与流程的切分-CU网元的信令
makefile文件编译
Network In Network学习记录
Edge 提供了标签分组功能