当前位置:网站首页>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,对init
Increase 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,进入下一循环.
边栏推荐
- matlab 读取csv文件绘图
- 单点登录的三种方式
- Source code analysis of GZIPInputStream class
- Hospital management system database, course design, SQLserver, pure code design
- SQL——左连接(Left join)、右连接(Right join)、内连接(Inner join)
- C#之泛型、委托、事件及其使用
- csdn文件导出为pdf
- 半个月时间把MySQL重新巩固了一遍,梳理了一篇几万字 “超硬核” 文章!
- Emotional crisis, my friend's online dating girlfriend wants to break up with him, ask me what to do
- 梅科尔工作室--鸿蒙十四天开发培训笔记(八)
猜你喜欢
随机推荐
金鱼哥RHCA回忆录:CL210管理OPENSTACK网络--开放虚拟网络(OVN)简介(课后练习)
NowCoderTOP28-34二叉树——持续更新ing
GZIPInputStream 类源码分析
掌握SSR
Redis Cluster - Sentinel Mode Principle (Sentinel)
Meikle Studio--Hongmeng 14-day development training notes (8)
【23提前批】北森云计算-测开面经
7 天学个Go,Go 结构体 + Go range 来学学
Redis缓存面临的缓存击穿问题
web安全入门-黑苹果MAC系统安装
SQL——左连接(Left join)、右连接(Right join)、内连接(Inner join)
我们能做出来数据库吗?
Dart Log tool class
一种用于保证多方子系统数据一致性的方法
Single sign-on principle and implementation
Insertion and deletion of doubly linked list
NowCoderTOP17-22 二分查找/排序——持续更新ing
The big-eyed Google Chrome has also betrayed, teach you a trick to quickly clear its own ads
Build finished with errors/Executable Not Found
Sql optimization summary!detailed!(Required for the latest interview in 2021)