if  else 结构:

#第一种
if condition {
// do something
}
#第二种
if condition {
// do something
} else {
// do something
}
#第三种
if condition1 {
// do something
} else if condition2 {
// do something else
}else {
// catch-all or default
}
#注意大括号的位置,都是固定的
if在condition 中还可以加上赋值语句

if val := 10; val > max {
 // do something
 }

switch 结构

#第一种
switch var1 {
case val1:
...
case val2:
...
default:
...
}
#第二种
switch {
case i < 0:
f1()
case i == 0:
f2()
case i > 0:
f3()
} #第三种 switch result := calculate(); {
case result < 0:
...
case result > 0:
...
default:
// 0
}

for 循环

#第一种
for i:=0; i<5; i++ {
for j:=0; j<10; j++ {
println(j)
}
}
#第二种
for a < b {
a++
fmt.Printf("a 的值为: %d\n", a)
}
# 类似其他语言for和while
无限循环
for true {} 或者for {} 这种内部需要有条件判断来退出循环

for range 结构

这是 Go 特有的一种的迭代结构,您会发现它在许多情况下都非常有用。它可以迭代任何一个集合(包括数组和 map,详见第 7 和 8 章)。语法上很类似其它语言中 foreach 语句,但您依旧可以获得每次迭代所对应的索引。一般形式为: for ix, val := range coll { }  。要注意的是, val  始终为集合中对应索引的值拷贝,因此它一般只具有只读性质,对它所做的任何修改都不会影响到集合中原有的值(译者注:如果  val  为指针,则会产生指针的拷贝,依旧可以修改集合中的原值)

func main() {
s :="hello world"
for in,va := range s{
fmt.Print(in,va,"\n")
}
} #va 索引值的拷贝
0 104
1 101
2 108
3 108
4 111
5 32
6 119
7 111
8 114
9 108
10 100

关键字fallthrough

在switch语句中,如果一个case语句后面加了fallthrough那么会直接执行下一个case里面的内容

func main() {
switch Num:=;{
case <= Num && Num <= :
fmt.Printf("0-3")
fallthrough
case <= Num && Num <= :
fmt.Printf("4-6")
case <= Num && Num <= :
fmt.Printf("7-9")
}
}
--

最新文章

  1. 数字限时增长效果实现:numberGrow.js
  2. 常用的WebForm 控件
  3. javascript Date
  4. TP5的图片上传
  5. redis 集群环境搭建-redis集群管理
  6. hdu1059 Dividing ——多重背包
  7. 关于favicon.ico的使用
  8. java web 学习九(通过servlet生成验证码图片)
  9. T-SQL 使用链接库向mysql导数据遇到的奇葩事件一
  10. 【BZOJ】1015: [JSOI2008]星球大战starwar
  11. UVa 10491 Cows and Cars (概率&amp;广义三门问题 )
  12. folly教程系列之:future/promise
  13. 如何在Unity中分别实现Flat Shading(平面着色)、Gouraud Shading(高洛德着色)、Phong Shading(冯氏着色)
  14. spring项目读取配置文件
  15. Android插件化的思考——仿QQ一键换肤,思考比实现更重要!
  16. git 使用简易指南
  17. 举例分析 Makefile 中的 patsubst、wildcard、notdir 函数
  18. Python+Appium学习篇之WebView处理
  19. eclipse调试断点【转载】
  20. 使用TELNET手工操作 IMAP 查看邮件

热门文章

  1. Address already in use : connect
  2. UIGestureRecognizer 手势
  3. 浏览器报406 错误:The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request &quot;accept&quot; headers
  4. unity DOTween Pro的使用--简化流程--自动播放
  5. BZOJ3926 诸神眷顾的幻想乡
  6. 03.父工程pom、整合测试、SpringBootApplication注解
  7. Python 石头 剪刀 布
  8. vue中v-model详解
  9. keras及神经网络,以简单实例入门
  10. pgrep,pidof工具的使用