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!")
}