当前位置: 首页 > article >正文

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.kler.cn/news/304121.html

相关文章:

  • Spring Boot Admin集成与自定义监控告警
  • 【C++ 高频面试题】指针和引用、关于内存泄漏和野指针问题
  • 云服务器中的MinIO 配置 HTTPS 过程(图文)
  • 基于微信小程序+Java+SSM+Vue+MySQL的药店管理系统
  • Iceberg与SparkSQL查询操作整合
  • JS设计模式之适配器模式:接口天然的“翻译官”
  • 【物联网技术大作业】设计一个智能家居的应用场景
  • [项目][WebServer][项目介绍及知识铺垫][下]详细讲解
  • Java项目: 基于SpringBoot+mybatis+maven美发门店管理系统(含源码+数据库+毕业论文)
  • 【HTTP】URL的基本概念和构成
  • Unity Lua方向的面试真题详解
  • 阿里巴巴商品详情API返回值:电商精准营销的关键
  • Go语言概述
  • 人力资源管理系统员工组织与微软AD域服务系统集成案例
  • HOT 100(七)栈、堆、贪心算法
  • 游戏工作室搬砖多开怎么做
  • 一篇文章了解Pytest单元测试框架
  • openai最新模型o1全面解读
  • HarmonyOS Next鸿蒙NDK使用示例
  • Rust 数据类型
  • 【开发工具】java开发中让你版本管理不在复杂的插件:GitToolBox
  • 【60天备战软考高级系统架构设计师——第十八天:运维与服务管理——DevOps实践】
  • 使用HTML
  • Python知识点:如何使用Vagrant进行开发环境搭建
  • 1.1 计算机网络基本概述
  • 代理IP的全面解析
  • OpenHarmony(鸿蒙南向开发)——轻量系统STM32F407芯片移植案例
  • CLUSTERDOWN Hash slot not served问题复现
  • react 事件处理
  • 前端——标签二(超链接)