I haven't seen all the discussion referenced, but I remember digging deep into the python language archives where Guido and others eventually relented and added ternaries (with the syntax "a if val else b"). I can't remember the argument which swung the consensus but the arguments against seem remarkably similar.

Go does have a one-line ternary:

var result = map[bool]string{false: "a", true: "b"}[test]

It's less of a hack if you declare the lookup "table" separately.

Sam

On Aug 14, 2018 9:52 PM, Agniva De Sarker <agniva.quicksil...@gmail.com> wrote:
Your answer is here - https://tip.golang.org/doc/faq#Does_Go_have_a_ternary_form.


On Tuesday, 14 August 2018 22:13:37 UTC+5:30, 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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
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.

Reply via email to