当前位置:网站首页>go语言的goto语句

go语言的goto语句

2022-06-11 20:54:00 只是六号z

goto语句

goto语句

goto:可以无条件地转移到过程中指定的行。

语法结构:

goto label;
..
..
label: statement;

在这里插入图片描述

使用goto语句可以统一错误处理

多处错误处理存在代码重复时是十分棘手的,例如:

	err :=  firstCheckError()
	if err != nil {
    
		goto onExit
	}
	err = secondChechError()
	if err != nil {
    
		goto onExit
	}
	return
onExit:
	fmt.Println(Err)
	exitProcess()

原网站

版权声明
本文为[只是六号z]所创,转载请带上原文链接,感谢
https://blog.csdn.net/z318913/article/details/125175054