Skip to content

Instantly share code, notes, and snippets.

@macu
Created January 12, 2015 16:40
Show Gist options
  • Save macu/824efeec5bd6937da19f to your computer and use it in GitHub Desktop.
Save macu/824efeec5bd6937da19f to your computer and use it in GitHub Desktop.
I hadn't realized you could access variables declared in if statements beyond the first block following the expression. This enables lots of code cleanup :)
package main
import "fmt"
func fab() (int, error) {
return 0, nil
}
func main() {
if i, err := fab(); err != nil {
// Handle error
fmt.Println("err!")
} else if i < 0 {
// Validate
fmt.Println("bad")
} else {
// Handle value
fmt.Println(i)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment