Go语言现代web开发08 if和switch分支语句

if语句

If is the most common conditional statement in programming languages. If the result of the condition caculation is positive(true), the code inside if statement will be executed. In the next example, value a will be incremented if it is less than 100.

If是编程语言中最常见的条件语句。如果条件计算的结果为正(true),则执行If语句中的代码。在下一个示例中,如果值a小于100,则值a将递增。

if a < 100 {a += 1
}

We can add a short statement before the condition. This statement will be executed before the condition, and the declared variable will be visiable only in the scope of if statement. Here is an example, where variable a will be incremented, only if the previously calculated value for variable b is less than 100:

我们可以在条件前加一个简短的语句。该语句将在条件之前执行,并且声明的变量仅在if语句的作用域中可见。下面是一个例子,只有当变量b的先前计算值小于100时,变量a才会增加:

if b := a * a; b < 100 {a += 1
}

We can add the else statement to if statement. Code inside the else statement will be executed if the result of the condition execution is negative (false). In the next example, if the value of variable a is less than 100, the value for a will be incremented, otherwise value a will be multiplied by 5.

我们可以将else语句添加到if语句中。如果条件执行的结果为负(false), else语句中的代码将被执行。在下一个示例中,如果变量a的值小于100,则a的值将增加,否则a的值将乘以5。

if a < 100 {a += 1
} else {a *= 5
}

Additionally, we can append if-else statements, but that code will not be readable. If we have a need to create such a construct, it is better to use the switch statement (we will see this statement later). Here is an example of an if-else statement that will return the country name based on the contry code.

此外,我们可以附加if-else语句,但这些代码将不具有可读性。如果需要创建这样的结构,最好使用switch语句(稍后将看到该语句)。下面是一个if-else语句的示例,它将根据国家代码返回国家名称。

if code == "fr" {country = "France"
} else if code == "uk" {country = "United Kingdom"
} else {country = "India"
}

switch 语句

The switch statement is an elegant way to avoid the usage of if-else sequences.

switch语句是避免使用if-else序列的一种优雅方式。

The sequence of if-else statements from the previous example can be replaced with the switch statement.

前面示例中的if-else语句序列可以替换为switch语句。

var country string
switch code {case "fr":country = "France"case "uk":country = "United Kingdom"default:country = "India"
}

The first case statement whose value is equivalent to the condition expression will be executed. If the value of the code variable is equal to fr, the first case statement will be executed. In some programming languages, all following case statements will be executed unless we put the break keyword at the end of the case statement. In the Go programming language, only the selected case statement will be executed(break is provided automatically). If none of the case statements match the condition, the default statement will be executed.

第一个与条件表达式值相等的case语句将被执行。如果code变量的值等于fr,则执行第一个case语句。在某些编程语言中,除非将break关键字放在case语句的末尾,否则将执行以下所有case语句。在Go编程语言中,只执行选定的case语句(自动提供break)。如果所有case语句都不符合条件,则执行默认语句。

Usually, switch cases must be constants and all involved values must be integers. Go programming language is much more flexible. We can even use a function call in case statements! It is possible to omit a condition from the switch statement and move it to the case statement. In that situation, the case statement shose condition is fulfilled will be executed. This condition-less switch statement will determine if the number is even or add:

通常,switch case必须是常量,并且所有涉及的值都必须是整数。Go编程语言更加灵活。我们甚至可以在case语句中使用函数调用!可以从switch语句中省略条件,并将其移到case语句中。在这种情况下,将执行条件满足的case语句。这个无条件的switch语句将确定数字是偶数还是加法:

switch {case number % 2 == 0:fmt.Println("Even number!")case number % 2 == 1:fmt.Println("Odd number!")default:fmt.Println("Invalid number!")
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/bicheng/54179.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

《黑神话:悟空》:中国游戏界的新篇章

引言&#xff1a; 在数字娱乐的浪潮中&#xff0c;游戏已成为连接全球文化的重要媒介。 《黑神话&#xff1a;悟空》的问世&#xff0c;不仅是中国游戏产业的一个里程碑&#xff0c;更是文化自信的闪耀展现。 这款游戏以其独特的艺术风格和深刻的文化内涵&#xff0c;在全球范…

k8s中的认证授权

目录 一、kubernetes API 访问控制 1.1 UserAccount与ServiceAccount 1.1.1 ServiceAccount 1.1.2 ServiceAccount示例 二、认证(在k8s中建立认证用户) 2.1 创建UserAccount 2.2 RBAC&#xff08;Role Based Access Control&#xff09; 2.2.1 基于角色访问控制授权&…

RT-DETR改进策略:BackBone改进|使用StarNet改进RT-DERT,显著提升性能与效率

摘要 本文介绍了我们如何将最新的StarNet模型成功应用于实时目标检测任务中,特别是用于改进RT-DERT(一种高效的实时目标检测网络)的主干网络部分。通过详尽的实验和理论分析,我们证明了StarNet不仅能够显著增强RT-DERT的检测精度,同时保持了高效的计算性能和低延迟特性。…

C++从入门到起飞之——继承下篇(万字详解) 全方位剖析!

&#x1f308;个人主页&#xff1a;秋风起&#xff0c;再归来~&#x1f525;系列专栏&#xff1a;C从入门到起飞 &#x1f516;克心守己&#xff0c;律己则安 目录 1、派⽣类的默认成员函数 1.1 四个常⻅默认成员函数 1.2 实现⼀个不能被继承的类 ​编辑 2. 继承与友…

力扣题解2390

大家好&#xff0c;欢迎来到无限大的频道。 今日继续给大家带来力扣题解。 题目描述​&#xff08;中等&#xff09;&#xff1a; 从字符串中移除星号 给你一个包含若干星号 * 的字符串 s 。 在一步操作中&#xff0c;你可以&#xff1a; 选中 s 中的一个星号。 移除星号…

力扣刷题(6)

两数之和 II - 输入有序数组 两数之和 II - 输入有序数组-力扣 思路&#xff1a; 因为该数组是非递减顺序排列&#xff0c;因此可以设两个左右下标当左右下标的数相加大于target时&#xff0c;则表示右下标的数字过大&#xff0c;因此将右下标 - -当左右下标的数相加小于targ…

HashMap线程不安全|Hashtable|ConcurrentHashMap

文章目录 常见集合线程安全性HashMap为什么线程不安全&#xff1f;怎么保证HashMap线程安全 HashtableConcurrentHashMap 引入细粒度锁代码中分析总结 小结 常见集合线程安全性 ArrayList、LinkedList、TreeSet、HashSet、HashMap、TreeMap等都是线程不安全的。 HashTable是线…

C语言:数组

1. 数组的概念 数组是⼀组相同类型元素的集合&#xff1b;从这个概念中我们就可以发现2个有价值的信息&#xff1a; • 数组中存放的是1个或者多个数据&#xff0c;但是数组元素个数不能为0。 • 数组中存放的多个数据&#xff0c;类型是相同的。 数组分为⼀维数组和多维数组…

【C语言必学知识点七】你知道在动态内存管理中存在的内存泄露问题吗?遇到内存泄露时应该如何处理?今天跟你好好介绍一下如何正确使用calloc与realloc!!!

动态内存管理——动态函数&#xff08;calloc、realloc&#xff09;的使用 导读一、calloc函数1.1 函数介绍1.2 calloc的使用1.3 calloc与malloc 二、realloc函数2.1 函数介绍2.2 realloc的使用2.3 realloc的空间分配2.3.1 空间分配成功——地址的改变2.3.2 空间分配失败——内…

【在Linux世界中追寻伟大的One Piece】数据链路层

目录 1 -> 数据链路层 2 -> 对比理解“数据链路层”和“网络层” 3 -> 以太网 3.1 -> 以太网的帧格式 4 -> 认识MAC地址 4.1 -> 对比理解MAC地址和IP地址 5 -> 认识MTU 5.1 -> MTU对IP协议的影响 5.2 -> MTU对UDP协议的影响 5.3 -> MT…

ElasticSearch介绍+使用

ElasticSearch 1.背景 ElasticSearch的最明显的优势在于其分布式特性&#xff0c;能够扩展到上百台服务器&#xff0c;极大地提高了服务器的容错率。在大数据时代背景下&#xff0c;ElasticSearch与传统的数据库相比较&#xff0c;能够应对大规模的并发搜索请求&#xff0c;同…

JavaScript网页设计案例分析

JavaScript网页设计案例分析 随着互联网技术的发展&#xff0c;JavaScript 已经成为现代网页设计中不可或缺的一部分。从简单的页面交互到复杂的应用程序开发&#xff0c;JavaScript 都发挥着至关重要的作用。本文将探讨几个运用 JavaScript 进行网页设计的经典案例&#xff0…

使用Python实现多个PDF文件的合并

使用Python可以很方便地实现多个PDF文件的合并。我们可以使用PyPDF2库来完成这个任务。以下是一个实现PDF合并的Python脚本&#xff1a; import os from PyPDF2 import PdfMergerdef merge_pdfs(input_dir, output_filename):# 创建一个PdfMerger对象merger PdfMerger()# 获取…

UE5源码Windows编译、运行

官方文档 Welcome To Unreal Engine 5 Early Access Learn what to expect from the UE5 Early Access program. 链接如下&#xff1a;https://docs.unrealengine.com/5.0/en-US/Welcome/#gettingue5earlyaccessfromgithub Step 0&#xff1a;找到UE5源码 直接先上链接 https…

【Qt】Qml界面中嵌入C++ Widget窗口

1. 目的 qml做出的界面漂亮&#xff0c;但是执行效率低&#xff0c;一直想找一个方法实现qml中嵌入c界面。现在从网上找到一个方法&#xff0c;简单试了一下貌似可行&#xff0c;分享一下。 2. 显示效果 3. 代码 3.1 工程结构 3.2 pro文件 需要添加widgets > QT quick …

店群合一模式下的社区团购新发展——结合链动 2+1 模式、AI 智能名片与 S2B2C 商城小程序源码

摘要&#xff1a;本文探讨了店群合一的社区团购平台在当今商业环境中的重要性和优势。通过分析店群合一模式如何将互联网社群与线下终端紧密结合&#xff0c;阐述了链动 21 模式、AI 智能名片和 S2B2C 商城小程序源码在这一模式中的应用价值。这些创新元素的结合为社区团购带来…

设计模式重新整理

系统整理 河北王校长的 贯穿设计模式 和 王争的设计模式之美&#xff0c;希望能形成肌肉记忆 文章目录 为什么需要掌握设计模式1. 六大原则介绍1. 单一职责原则2. 开闭原则3. 里式替换原则4. 依赖倒置原则5. 接口隔离原则6. 迪米特法则 分类 单例模式适配器模式封装有缺陷的接口…

【可测试性实践】C++ 单元测试代码覆盖率统计入门

引言 最近在调研C工程怎么做单元测试和代码覆盖率统计&#xff0c;由于我们工程有使用Boost库&#xff0c;尝试使用Boost.Test来实现单元测试并通过Gcov和Lcov来生成代码覆盖率报告。本文记录完整的搭建测试Demo&#xff0c;希望能带来一定参考。 常用C单测框架对比 特性Goo…

嵌入式通信原理—SPI总线通信原理与应用

文章目录 SPI 简介基本原理工作模式特点 SPI寻址方式1. 片选&#xff08;Chip Select, CS&#xff09;2. 多从设备通信3. 菊花链&#xff08;Daisy-Chain&#xff09;模式4. 地址寄存器&#xff08;应用层&#xff09; SPI通信过程时钟信号生成&#xff08;SCLK&#xff09;数据…

9.15javaweb项目总结

1.贴吧界面算是完成了基本的 能通过url打开多个贴吧信息的界面了&#xff0c;界面水平不是很高&#xff0c;界面还有待提升&#xff0c;然后该界面的功能点还差点有点远&#xff0c;完成度不是很高。 2.解决了关注的功能问题 要考虑的地方有点多&#xff0c;最简单的就是点击…