当前位置:网站首页>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
边栏推荐
猜你喜欢
基于机器学习之模型树短期负荷预测(Matlab代码实现)
【物理应用】基于El-centro地震波作用下隔震与非隔震支座下的顶层位移、速度、加速度的对比情况附matlab代码
全文翻译:EDPB数据保护影响评估(DPIA:Data Protection Impact Assessment)指南
软件测试的调用接口怎么调用,逻辑是什么?
jetson nano 开机闪一下然后黑屏
LeetCode每日两题02:第一个错误的版本 (均1200道)方法:二分查找
torchversion.transforms的使用
[Signal denoising] Based on Sage-Husa adaptive Kalman filter to realize the suppression of ocean wave magnetic field noise and the generation of ocean wave magnetic field noise with matlab code
4-3 Matplotlib库 条形图
《Go语言学习:基本变量与类型》
随机推荐
Grid布局介绍
RS&FSW测试脚本
【Unity】判断鼠标是否点击在UI上
Go-10-模块与包
Dapr学习(4)之eShopOnDapr部署(Rancher2.63&k3s)
企业里Foxmail邮箱问题解决方法汇总
【图像增强】基于Step和Polynomial 滤波实现图像增强附matlab代码
5-2 Seaborn 分类绘图
TCP/IP协议栈
typescript89-展示任务列表功能
Node.js:MySQL.js的基本操作增删改查
《Go语言学习:基本变量与类型》
轻量化神经网络--MobileNet v3学习记录
4-2 Matplotlib库 基本使用(绘制折线图)
谷歌翻译软件-免费谷歌翻译
4-7 Matplotlib库 箱线图
数字孪生+燃气管理,开启智慧燃气管理新模式
睿智的目标检测61——Tensorflow2 Focal loss详解与在YoloV4当中的实现
数据恢复软件EasyRecovery支持恢复所有类型的文件
Qt中QFile、QByteArray QDataStream和QTextStream区别