当前位置:网站首页>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,进入下一循环.
边栏推荐
猜你喜欢
随机推荐
双链表的创建
Chapter Six
SQLServer2019安装(Windows)
顺序表的删除
面试、工作中常用sql大全(建议收藏备用)
Add a shuffling effect to every pie
C#之泛型、委托、事件及其使用
IBM SPSS Statistics 28软件安装包下载及安装教程
Chapter VII
【GORM】存取数组/自定义类型数据
GZIPInputStream 类源码分析
新人学习小熊派华为iot介绍
掌握SSR
KVM虚拟化作业
【云原生监控系列第一篇】一文详解Prometheus普罗米修斯监控系统(山前前后各有风景,有风无风都很自由)
Windows安装mysql详细步骤(通俗易懂,简单上手)
【LeetCode】1161.最大层内元素和
【LeetCode】21. 合并两个有序链表
NowCoderTOP23-27二叉树遍历——持续更新ing
C#多态的实现









