-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: Go 2: extend "goto" - statement #53074
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
For the example block of code:
The first instance seems to jump to the label |
walkthrough:
|
Note that in the example code we normally would not permit |
It is already possible, if a bit verbose to do goto-based error handling. If go had an if qualifier like Ruby, we could shorten this to https://play.golang.com/p/59VJFBaYnt4 package main
import (
"fmt"
"errors"
)
func errorEven(i int) error {
if i% 2 == 0 {
return errors.New("even")
}
return nil
}
func errorGoto(i int) {
var err error
fmt.Printf("Start: %d\n", i)
err = errorEven(i)
if err != nil {
goto err
}
goto ok
err:
fmt.Printf("There was an error: %s\n", err)
return
ok:
fmt.Println("All is ok")
return
}
func main() {
errorGoto(1)
errorGoto(2)
errorGoto(3)
errorGoto(4)
} |
a proposal scheme
|
This proposal overloads variable names and label names, such that the name of a label corresponds to the name of a variable. That is confusing, and would seem to work poorly if one wants to assign to a variable more than once. The emoji voting on this issue is not in favor. Therefore, this is a likely decline. Leaving open for four weeks for final comments. |
No further comments. |
Author background
intermediate -> experienced Go programmer?
n/a
Related proposals
Has some similarities to
https://go.dev/issue/32611
https://go.dev/issue/34140
https://go.dev/issue/37035
by making use of an extended "goto" - statement funcionality
yes
n/a
no
n/a
Proposal
https://groups.google.com/g/golang-nuts/c/5ARY8Pdu3zM
writers and maintainers of code bases with restructuring "if err != nil {}" by inlining func evaluations
compiler functionality
goto has extended functionality
In Go the "goto" statement transfers control to the statement with the corresponding label within the same function
new:
when put before https://go.dev/ref/spec#Expressions it takes all evaluated values (in lexical right-to-left order; ignoring the zero value for its type), jumps to a label with the same name as the "value holder" (if present) and holds a pointer to the corresponding value.
yes
n/a
scope?
https://go.dev/ref/spec#Goto_statements
n/a
n/a
n/a
Costs
The text was updated successfully, but these errors were encountered: