当前位置:网站首页>Find a Go job in 7 days, Conditional statements to learn in Gopher, loop statements, Part 3
Find a Go job in 7 days, Conditional statements to learn in Gopher, loop statements, Part 3
2022-07-31 10:37:00 【dream eraser】
本篇博客重点内容
来到 Go 语言学习的第 3 篇博客,This article will cover two Go 语句,One is branching,That is, conditional judgment,The second is the cycle.
Conditional judgment statements are further subdivided into if 语句,if else 语句,if 嵌套语句,switch 语句,select 语句,对于以往的 Python 学习者,
The above only switch 和 select Sentences need to be learned over and over again.
select 语句类似于 switch 语句,只是 select 会随机选择一个可运行的 case 执行.
Due to experience in other languages,The focus here is on the grammatical structure.
if 语句
语法结构如下所示:
if 布尔表达式{
/* Content to be executed */
}
带上 else 之后,语法结构如下所示:
if 布尔表达式{
/* TODO */
}
else{
/* TODO */
}
if Nesting related knowledge is consistent with other languages,可以直接学习.
switch 语句
该语句在 Python 中不存在,But there are basically other languages,语法结构如下所示:
switch vari{
case vari1:
//TODO
case vari2:
//TODO
}
switch 的 case 子句,默认带 break 语句,So it matches any of the branches,will terminate the statement,进入 switch continue to run after the code.
switch Statements can also be used type-switch 来判断变量类型,语法结构如下所示:
switch var1.(type){
case type:
// TODO
case type:
// TODO
}
在 Go 语言中,case 是一个独立的代码块,And the execution will not be like C The same language continues to the next one case,如果希望实现,可以使用 fallthrough 关键字实现,But look at the experience of the eraser,This knowledge point does not need to be mastered deliberately,毕竟 switch 设计的初衷,Just don't want to cross case Operation occurs.
select 语句
select 与 switch 语句类似,It will randomly execute a runnable case,如果没有 case,就会阻塞,Here is a new concept,叫做 Go Channel,Let's reserve this part,Learn in detail later.
Go 循环
Look at the loop statement,There aren't too many special points,而且 Go 只支持 for 循环,Knowledge points have been reduced a bit,不过 Go 循环的语法与 Python 有一些差异,例如下述 Demo.
package main
import "fmt"
func main() {
sum := 0
for i := 0; i < 10; i++ {
sum += i
}
fmt.Println(sum)
}
Abstract the syntax structure,The following format is obtained:
for init;condition;post {
}
其中各参数说明如下:
init:赋值表达式,给控制变量赋值;condition:关系表达式或逻辑表达式,控制条件;post:is also an assignment expression,对initIncrease or decrease control.
其中 init 和 post 为可选参数,可以省略,That is, the following code works normally.
package main
import "fmt"
func main() {
sum := 1
for ;sum <= 20; {
sum += sum
}
fmt.Println(sum)
}
上述 for 语句后面的分号 ; 可以省略,即下述代码.
for sum <= 20{
sum += sum
}
This is different from other languages for The following content does not need parentheses,If you want to achieve something similar while 的无限循环,直接省略 for 后面的内容即可.
package main
func main() {
sum := 0
for {
sum++
if sum > 100 {
break
}
}
}
The following must be noted here,其中涉及了 Go 的语法格式.
- for 后面的大括号,必须与 for 在同一行;
- Go 语言中也支持 continue 和 break 控制循环,And there is a more advanced one break 用法,Can terminate the specified loop(That is, the termination label label 所在的循环);
package main
import "fmt"
func main() {
// 不使用标记 label
fmt.Println("使用 break 标记")
for i := 1; i <= 10; i++ {
fmt.Printf("i: %d\n", i)
}
// 使用标记 label
fmt.Println("不使用 break 标记")
re:
for i := 1; i <= 10; i++ {
fmt.Printf("i: %d\n", i)
if i > 5 {
break re
}
}
}
continue 标记与 break 用法一致,Just its meaning means to jump out of the current loop,进入下一循环.
边栏推荐
猜你喜欢
随机推荐
如何判断自己是否适合IT行业?方法很简单
web安全入门-黑苹果MAC系统安装
Summary of three methods for SQL deduplication
“chmod 777-R 文件名”什么意思?
Deletion of the sequence table
力扣shell刷题
Android安全专题(三)JNI混淆
C#多态的实现
How SQL intercepts specified characters from strings (three functions of LEFT, MID, RIGHT)
Windows安装mysql详细步骤(通俗易懂,简单上手)
GCD简单了解
Detailed explanation of SQL stored procedures
双链表的插入和删除
Gradle series - Groovy overview, basic use (based on Groovy document 4.0.4) day2-1
v-model的原理
DC-7-vulnhub
Chapter Six
我们能做出来数据库吗?
出色的移动端用户验证
centos7安装mysql5.7









