3 ms·
It's worse that that, since you need to declare the variable as well. It's the "var n int" piece that bugs me, since sometimes you're moving code around and ha
by koblas 10y ago
It's worse that that, since you need to declare the variable as well. It's the "var n int" piece that bugs me, since sometimes you're moving code around and have a n := trueVal hidden in there.
var n int
if expr {
n = trueVal
} else {
n = falseVal
}
- junke 10y agoGo has a ternary if, but the syntax is weird ;-) n := map[bool]func() int{ false: func() int { return falseExpr }, true: func() int { return trueExpr }, }[expr]() https://play.golang.org/p/TXIse32WD9 https://play.golang.org/p/TXIse32WD9 Now, let's see how to abstract it with "go generate"...