News for hackers. Stuff that matters.
Re:My Go Resolutions for 2017 by koblas on Thursday January 19, 2017 @08:44AM (#13435670)
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
    }
Re:My Go Resolutions for 2017 by junke on Thursday January 19, 2017 @09:09AM (#13435827)
Go 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

Now, let's see how to abstract it with "go generate"...