I long for a ternary which disallows embedded ternaries. I use this one-liner, but not happily:
v := a; if t { v = b } This is not compatible with go fmt, but that tool's effects are undocumented (see issue 18790 <https://github.com/golang/go/issues/18790> which was declined), and it has no switches to disable/enable features. A syntax-aware sed is a good idea, but sadly go fmt destroys useful constructs. Other examples: err := fn() if err != nil { return err } switch v { case 1: pkg.one() case 2: pkg.two() case 3: pkg.thrice() } On Tuesday, August 14, 2018 at 9:43:37 AM UTC-7, Mark Volkmann wrote: > > I’m new to Go and I imagine the idea of adding a ternary operator to Go > has been discussed many times. Rather than repeat that, can someone point > me to a discussion about why Go doesn’t add this? I’m struggling to > understand why it is desirable to write code like this: > > var color > if temperature > 100 { > color = “red” > } else { > color = “blue” > } > > Instead of this: > > var color = temperature > 100 ? “red” : “blue” > > Is the ternary really so confusing that it justifies writing 6 lines of > code instead of 1? I realize I could eliminate two lines like the > following, but this isn’t a good idea if the values come from function > calls since there would sometimes be needless function calls. > > var color = “blue” > if temperature > 100 { > color = “red” > } > > --- > R. Mark Volkmann > Object Computing, Inc. -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.