mysql goto_11. Go 语言流程控制:goto 无条件跳

Hi,大家好,我是明哥。

在自己学习 Golang 的这段时间里,我写了详细的学习笔记放在我的个人微信公众号 《Go编程时光》,对于 Go 语言,我也算是个初学者,因此写的东西应该会比较适合刚接触的同学,如果你也是刚学习 Go 语言,不防关注一下,一起学习,一起成长。

我的在线博客:http://golang.iswbm.com

我的 Github:github.com/iswbm/GolangCodingTime

Go里的流程控制方法还是挺丰富,整理了下有如下这么多种:

if - else 条件语句

switch - case 选择语句

for - range 循环语句

goto 无条件跳转语句

defer 延迟执行

前面三种,我已经都讲过了,今天要讲讲 goto 的无条件跳转。

很难想象在 Go 居然会保留 goto,因为很多人不建议使用 goto,所以在一些编程语言中甚至直接取消了 goto。

我感觉 Go 既然保留,一定有人家的理由,只是我目前还没感受到。不管怎样,咱还是照常学习吧。

0. 基本模型

goto 顾言思义,是跳转的意思。

goto 后接一个标签,这个标签的意义是告诉 Go程序下一步要执行哪里的代码。

所以这个标签如何放置,放置在哪里,是 goto 里最需要注意的。

goto 标签;

...

...

标签: 表达式;

1. 最简单的示例

goto 可以打破原有代码执行顺序,直接跳转到某一行执行代码。

import "fmt"

func main() {

goto flag

fmt.Println("B")

flag:

fmt.Println("A")

}

执行结果,并不会输出 B ,而只会输出 A

A

2. 如何使用?

goto 语句通常与条件语句配合使用。可用来实现条件转移, 构成循环,跳出循环体等功能。

这边举一个例子,用 goto 的方式来实现一个打印 1到5 的循环。

import "fmt"

func main() {

i := 1

flag:

if i <= 5 {

fmt.Println(i)

i++

goto flag

}

}

输出如下

1

2

3

4

5

再举个例子,使用 goto 实现 类型 break 的效果。

import "fmt"

func main() {

i := 1

for {

if i > 5 {

goto flag

}

fmt.Println(i)

i++

}

flag:

}

输出如下

1

2

3

4

5

最后再举个例子,使用 goto 实现 类型 continue的效果,打印 1到10 的所有偶数。

import "fmt"

func main() {

i := 1

flag:

for i <= 10 {

if i%2 == 1 {

i++

goto flag

}

fmt.Println(i)

i++

}

}

输出如下

2

4

6

8

10

3. 注意事项

goto语句与标签之间不能有变量声明,否则编译错误。

import "fmt"

func main() {

fmt.Println("start")

goto flag

var say = "hello oldboy"

fmt.Println(say)

flag:

fmt.Println("end")

}

编译错误

.\main.go:7:7: goto flag jumps over declaration of say at .\main.go:8:6

系列导读

bVbFOSP

有疑问加站长微信联系(非本文作者)

5c5fbae790ec0313d6ee17e8b3dd9ba1.png

func (task *MySQLClusterTopologyTask) makeClusterInfo(insts []*dao.MySQLInstance) *dao.MySQLCluster { // 状态信息最为复杂 var mc *dao.MySQLCluster statusCntMap := map[common.MySQLInstanceStatus]int{} status2CntMap := map[common.MySQLInstanceStatus2]int{} var masterAbnormalCnt, slaveAbnormalCnt, masterResourceLimitedCnt, slaveResourceLimitedCnt, stoppedCnt, leftCnt, lanencyCnt int for _, inst := range insts { if inst.Role == common.MySQLInstanceRoleMaster { mc = &dao.MySQLCluster{ ClusterId: inst.ClusterId, MasterInstanceId: inst.InstanceId, MasterIP: inst.IP, MasterPort: inst.Port, Source: inst.Source, ClusterName: task.generateClusterId(), ClusterArch: inst.ClusterArch, } } status := inst.Status if _, ok := statusCntMap[status]; !ok { statusCntMap[status] = 1 } else { statusCntMap[status] += 1 } status2 := inst.Status2 if _, ok := status2CntMap[status2]; ok { status2CntMap[status2] = 1 } else { status2CntMap[status2] += 1 } if inst.Role == common.MySQLInstanceRoleMaster { if inst.Status == common.MySQLInstanceStatusAbnormal { masterAbnormalCnt += 1 } if inst.Status == common.MySQLInstanceStatusRunLimit { masterResourceLimitedCnt += 1 } } else { if inst.Status == common.MySQLInstanceStatusAbnormal { slaveAbnormalCnt += 1 } if inst.Status == common.MySQLInstanceStatusRunLimit { slaveResourceLimitedCnt += 1 } } if inst.Status == common.MySQLInstanceStatusStopped { stoppedCnt += 1 } if inst.Status2 == common.MySQLInstanceStatus2NodeLeftCluster { leftCnt += 1 } if inst.LatencySeconds > 0 { lanencyCnt += 1 } } if mc == nil { // 说明实例列表中只有集群的slave节点,主节点可能是一阶邻居,这样的集群不记录在平台的集群信息表中 return nil } // 计算集群主状态 nodeCnt := len(insts) if cnt, ok := statusCntMap[common.MySQLInstanceStatusStopped]; ok { if cnt == nodeCnt { mc.Status = common.MySQLClusterStatusStopped goto Return } } if cnt, ok := statusCntMap[common.MySQLInstanceStatusOffline]; ok { if cnt == nodeCnt { mc.Status = common.MySQLClusterStatusOffline goto Status2 } } if _, ok := statusCntMap[common.MySQLInstanceStatusMaintenance]; ok { mc.Status = common.MySQLClusterStatusMaintenance goto Status2 } if masterAbnormalCnt > 0 { mc.Status = common.MySQLClusterStatusAbnormal goto Status2 } if masterResourceLimitedCnt+slaveResourceLimitedCnt+stoppedCnt+leftCnt+slaveAbnormalCnt > 0 { mc.Status = common.MySQLClusterStatusRunlimited goto Status2 } if cnt, ok := statusCntMap[common.MySQLInstanceStatusRunning]; ok { if cnt == nodeCnt { mc.Status = common.MySQLClusterStatusRunning goto Status2 } } Status2: // 计算集群二级状态 if _, ok := status2CntMap[common.MySQLInstanceStatus2Upgrading]; ok { mc.Status2 = common.MySQLClusterStatus2Upgrading goto Return } if _, ok := status2CntMap[common.MySQLInstanceStatus2Backuping]; ok { mc.Status2 = common.MySQLClusterStatus2Backuping goto Return } if _, ok := status2CntMap[common.MySQLInstanceStatus2Restoring]; ok { mc.Status2 = common.MySQLClusterStatus2Restoring goto Return } if masterAbnormalCnt > 0 { mc.Status2 = common.MySQLClusterStatus2MasterAbnormal goto Return } if slaveAbnormalCnt > 0 { mc.Status2 = common.MySQLClusterStatus2SlaveAbnormal goto Return } if stoppedCnt > 0 { mc.Status2 = common.MySQLClusterStatus2NodeStopped goto Return } if leftCnt > 0 { mc.Status2 = common.MySQLClusterStatus2NodeLeft goto Return } if masterResourceLimitedCnt+slaveResourceLimitedCnt > 0 { mc.Status2 = common.MySQLClusterStatus2ResourceLimited goto Return } if lanencyCnt > 0 { mc.Status2 = common.MySQLClusterStatus2Delayed goto Return } Return: return mc } 帮我分析下 这段代码是做什么的
最新发布
10-18
:step4 echo %time% begin excute step4 >> %log_path% :: 循环导出 obj 数组中的表 set currentIndex=0 :exportStartLoop if %currentIndex% EQU %tableCounts% goto:exportEndLoop for /f "usebackq delims==. tokens=1-3" %%i in (`set obj[%currentIndex%]`) do ( set curObj.%%j=%%k ) set export_ingnore_table=%export_ingnore_table% --ignore-table=%database_scope%.%curObj.table% if %currentIndex% LSS %param_tableIndex% ( set /a currentIndex=%currentIndex% + 1 goto:exportStartLoop ) echo %time% begin export table %curObj.table% echo %time% begin export table %curObj.table% >> %log_path% set tryCount=0 :: 导出失败进行 3 次尝试 :exporttable if %tryCount% EQU 3 ( echo 导出表 %curObj.table% 失败 无法继续 请联系软件工程人员 echo. pause exit 0 ) if %param_day% EQU 0 ( mysqldump -h%mysqlServer% -u%mysqlUser% -p%mysqlPassword% --no-data --databases %database_scope% --tables %curObj.table% > %param_volume%\%dataSavePath%\%curObj.table%.sql 2>>%log_path% ) else ( mysqldump -h%mysqlServer% -u%mysqlUser% -p%mysqlPassword% --databases %database_scope% --tables %curObj.table% --where="%curObj.field%>date_sub(curdate(), interval %param_day% day)" > %param_volume%\%dataSavePath%\%curObj.table%.sql 2>>%log_path% ) if %errorlevel% NEQ 0 ( echo %time% failed to export table:%curObj.table% >> %log_path% set /a tryCount=%tryCount% + 1 goto:exporttable ) echo %time% end export table %curObj.table% echo. echo %time% end export table %curObj.table% >> %log_path% set /a currentIndex=%currentIndex% + 1 set /a param_tableIndex=%currentIndex% call:fun_save_param goto:exportStartLoop :exportEndLoop :: 导出 scope 库 set tryCount=0 :exportscope if %tryCount% EQU 3 ( echo 导出库 %database_scope% 失败 无法继续 请联系软件工程人员 echo. pause exit 0 ) if %database_scope%% NEQ null ( echo %time% begin export database %database_scope% echo %time% begin export database %database_scope% : %export_ingnore_table% >> %log_path% mysqldump -h%mysqlServer% -u%mysqlUser% -p%mysqlPassword% --databases --events --routines %export_ingnore_table%> %param_volume%\%dataSavePath%\%database_scope%.sql 2>>%log_path% echo %time% end export database %database_scope% echo. echo %time% end export database %database_scope% >> %log_path% ) if %errorlevel% NEQ 0 ( echo %time% failed to export database:%database_scope% >> %log_path% set /a tryCount=%tryCount% + 1 goto:exportscope ) set param_step=5 call:fun_save_param echo %time% end excute step4 >> %log_path% 什么意思
07-02
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值