Skip to content

Instantly share code, notes, and snippets.

@adamryman
Created November 1, 2016 18:36
Show Gist options
  • Save adamryman/52f89c6a33404fa403b406631b4367ec to your computer and use it in GitHub Desktop.
Save adamryman/52f89c6a33404fa403b406631b4367ec to your computer and use it in GitHub Desktop.
Trying out golang short circuits
package main
import "fmt"
func main() {
// false
fmt.Println(shortCircuit())
// panic
fmt.Println(brokenCircuit())
}
type optional interface {
Optional() bool
}
func shortCircuit() bool {
var test interface{}
opt, ok := test.(optional)
// Returns early, does not evaluate opt.Optional()
return false && opt.Optional() && ok
}
func brokenCircuit() bool {
var test interface{}
opt, ok := test.(optional)
// Calls opt.Optional, panics
return opt.Optional() && false && ok
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment