Go: spec: Feature request: add a ternary expression

Created on 23 Jun 2017  ·  3Comments  ·  Source: golang/go

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

Most helpful comment

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.

All 3 comments

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.

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!

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
         }
Was this page helpful?
0 / 5 - 0 ratings