Golang proposal for declaring variable for the outer scope in an "if" statement
IfStmt = "if" [ SimpleStmt ";" ] Expression [ PostIfStmt ";" ] Block [ "else" ( IfStmt | Block ) ] .
PostIfStmt = SimpleStmt .
PostIfStmt
can access variables defined in a IfStmt
. It allows declearing variables for the outer scope of a IfStmt
.
if t, err := time.Parse(time.RFC3339, "2006-01-02T15:04:05Z"); err != nil; t := t {
return nil, err
}
fmt.Prinln(t) // 2006-01-02 15:04:05 +0000 UTC
fmt.Println(err) // undefined
func f() (string, error) {
return "aaa", nil
}
if a, b := f(); b != nil; outerVar := a {
return errors.Wrap(b, "error"}
}
fmt.Printf("%v", a) //undefined
fmt.Printf("%v", b) //undefined
fmt.Printf("%v", outerVar) // print "aaa"
The following will not fit the Spec mentioned above.
func f() (string, error) {
return "aaa", nil
}
if a, b := f(); b != nil; a {
return errors.Wrap(b, "error"}
}
fmt.Printf("%v", a) //print "aaa"
fmt.Printf("%v", b) //undefined