Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ElimGotos ¶
ElimGotos removes any goto statements from stmts by rewriting them as conditionals and loops. A transformed syntax tree is returned; stmts is not modified.
Example ¶
code := ` package branchy func isGoodNumber(x int) bool { if x%3 == 0 { goto fail } else if x%5 == 0 { goto fail } return true fail: return false } ` // Parse the code fset := token.NewFileSet() file, err := parser.ParseFile(fset, "code.go", code, 0) if err != nil { panic(err) } // List of statements in the function's body body := file.Decls[0].(*ast.FuncDecl).Body.List // Rewrite the function body without goto statements newBody := ElimGotos(body) // Print the result config := printer.Config{Mode: printer.UseSpaces, Tabwidth: 4} config.Fprint(os.Stdout, token.NewFileSet(), newBody)
Output: gotofail := false if x%3 == 0 { gotofail = true } else { gotofail = x%5 == 0 } if !gotofail { return true } return false
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.