
Go 条件与循环
循环
与其他主要编程语言的差异
Go 语言仅支持循环关键字 for。可以从以下代码看出,循环时并没有小括号
for j := 7;j <= 9; j++
代码示例
while 条件循环
n := 0
/* while(n<5) */
for n < 5 {
t.Log(n)
n++
}
无限循环
/*while(true)*/
n := 0
for {
...
}
if 条件
与其他语言差别不大(这里condition结果为bool值)
if condition-1 {
} else if condition-2{
} else{
}
举例
func TestIfMultiSec(t *testing.T) {
if a := 1 == 1; a {
t.Log("1==1")
}
}
与其他主要编程语言的差异
- 支持变量赋值:
if var declaration; condition{
......
}
举例
func TestIfMultiSec(t *testing.T) {
if v,err := someFun(); err==nil{
t.Log("1==1")
}else{
t.Log("1==1")
}
}
switch 条件
func TestSwitchMultiCase(t *testing.T) {
for i := 0; i < 5; i++ {
switch i {
case 0, 2:
t.Log("Event")
case 1, 3:
t.Log("Odd")
default:
t.Log("it is not 0-3")
}
}
}
也可以这样写
func TestSwitchCaseCondition(t *testing.T) {
for i := 0; i < 5; i++ {
switch {
case i%2 == 0:
t.Log("Event")
case i%2 == 1:
t.Log("Odd")
default:
t.Log("unknow")
}
}
}
结果
在 case 中写 fallthrough
函数可以执行到下一个case大家可以自行测试
欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739
评价
排名
2
文章
657
粉丝
44
评论
93
docker中Sware集群与service
尘叶心繁 : 想学呀!我教你呀
一个bug让程序员走上法庭 索赔金额达400亿日元
叼着奶瓶逛酒吧 : 所以说做程序员也要懂点法律知识
.net core 塑形资源
剑轩 : 收藏收藏
映射AutoMapper
剑轩 :
好是好,这个对效率影响大不大哇,效率高不高
一个bug让程序员走上法庭 索赔金额达400亿日元
剑轩 : 有点可怕
ASP.NET Core 服务注册生命周期
剑轩 :
http://www.tnblog.net/aojiancc2/article/details/167
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:
50010702506256


欢迎加群交流技术