Skip to content

Golang proposal for declaring variable for the outer scope in an "if" statement

Notifications You must be signed in to change notification settings

edv1n/golang-if-outer-scope-var-decl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 

Repository files navigation

Golang proposal for declearing variable in outer scope in if statement

Golang proposal for declaring variable for the outer scope in an "if" statement

Spec

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.

Usage

Scoping error within if statement when invoking functions that return more than one result

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

Example

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"

Short handed var decl [alternative syntax]

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

About

Golang proposal for declaring variable for the outer scope in an "if" statement

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published