-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
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
} |
Doing something like:
... is pretty cumbersome for such a simple selector. There really ought to be:
... or:
The text was updated successfully, but these errors were encountered: