Created
November 1, 2016 18:36
-
-
Save adamryman/52f89c6a33404fa403b406631b4367ec to your computer and use it in GitHub Desktop.
Trying out golang short circuits
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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