Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spec: Feature request: add a ternary expression #20774

Closed
michaelsafyan opened this issue Jun 23, 2017 · 3 comments
Closed

spec: Feature request: add a ternary expression #20774

michaelsafyan opened this issue Jun 23, 2017 · 3 comments

Comments

@michaelsafyan
Copy link

Doing something like:

      if value != nil {
        return *value
      }
      return defaultValue

... is pretty cumbersome for such a simple selector. There really ought to be:

         value != nil ? *value : defaultValue

... or:

       cond value != nil,  *value, defaultValue
@bradfitz
Copy link
Contributor

It was an explicit design decision (for readability) that Go does not have ternary expressions.

Sometimes Go makes you write a few more lines, but we accept that cost for explicitness and legibility.

@mikioh mikioh changed the title Feature request: add a ternary expression spec: Feature request: add a ternary expression Dec 27, 2017
@nad2000
Copy link

nad2000 commented Nov 16, 2018

I am right now working on a piece where there are heaps of simple simple condition mapping. Ternary operator absence makes it sooo unreadable! It's so frustrating!

@joeky888
Copy link

Ternary operator makes code readable and clean for me.

This is how I did in Javascript:

  function getLevel(level) {
-            if (level <= 23100) {
-                return 23100
-            }
-            else if (level <= 24000) {
-                return 24000
-            }
-            else if (level <= 25200) {
-                return 25200
-            }
-            else if (level <= 26400) {
-                return 26400
-            }
-            else if (level <= 27600) {
-                return 27600
-            }
-            else if (level <= 28800) {
-                return 28800
-            }
-            else if (level <= 30300) {
-                return 30300
-            }
-            else if (level <= 31800) {
-                return 31800
-            }
-            else if (level <= 33300) {
-                return 33300
-            }
-            else if (level <= 34800) {
-                return 34800
-            }
-            else if (level <= 36300) {
-                return 36300
-            }
-            else if (level <= 38200) {
-                return 38200
-            }
-            else if (level <= 40100) {
-                return 40100
-            }
-            else if (level <= 42000) {
-                return 42000
-            }
-            else if (level <= 43900) {
-                return 43900
-            }
-            else {
-                return 45800
-            }
+            return  level <= 23100 ? 23100 :
+                    level <= 24000 ? 24000 :
+                    level <= 25200 ? 25200 :
+                    level <= 26400 ? 26400 :
+                    level <= 27600 ? 27600 :
+                    level <= 28800 ? 28800 :
+                    level <= 30300 ? 30300 :
+                    level <= 31800 ? 31800 :
+                    level <= 33300 ? 33300 :
+                    level <= 34800 ? 34800 :
+                    level <= 36300 ? 36300 :
+                    level <= 38200 ? 38200 :
+                    level <= 40100 ? 40100 :
+                    level <= 42000 ? 42000 :
+                    level <= 43900 ? 43900 : 45800
         }

@golang golang locked as resolved and limited conversation to collaborators Jan 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants