使用标签和 goto 语句是不被鼓励的:它们会很快导致非常糟糕的程序设计,而且总有更加可读的替代方案来实现相同的需求。
如果您必须使用 goto,应当只使用正序的标签(标签位于 goto 语句之后),但注意标签和 goto 语句之间不能出现定义新变量的语句,否则会导致编译失败。
// compile error goto2.go:8: goto TARGET jumps over declaration of b at goto2.go:8package mainimport "fmt"func main() {a := 1goto TARGET // compile errorb := 9TARGET:b += afmt.Printf("a is %v *** b is %v", a, b)}
