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

[Feature-Request/Proposal] Conditional Operator (error-handling) #28329

Closed
maverigz opened this issue Oct 23, 2018 · 2 comments
Closed

[Feature-Request/Proposal] Conditional Operator (error-handling) #28329

maverigz opened this issue Oct 23, 2018 · 2 comments
Labels
error-handling Language & library change proposals that are about error handling. FrozenDueToAge

Comments

@maverigz
Copy link

I am kind of new to GitHub and not sure if this is the right place to post this, but here it goes:

What version of Go are you using (go version)?

go version 1.10

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

(Ubuntu 18.04.1 LTS 64-bit)
GOARCH="amd64"
GOCACHE="/home/thommy/.cache/go-build"
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/thommy/dev/go"
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"

What did you do?

https://play.golang.org/p/gjlLvTJX1ev

What did you expect to see?

Just that what I saw.

What did you see instead?

That what I expected.

 

The Problem: I would like Golang to have a ternary (or conditional) operator. This would help so much in a lot of situations, especially error-handling.
I have a lot of:

if err != nil {
    err.Error() 
} else {
    doSomething()
}

in my code, when I could shorten it to:

!err ? err.Error() : doSomething()

AFAIK there would be no compatibility-issues or whatsoever, since it's just a new feature to the language. If you don't know or understand the operator, or simply don't want to use it, then don't. I'd like to have this feature in Go.
This would clean up my code (IMO), and would make it more readable (at least for me).

Let me know what you think. Thanks!

@ghost
Copy link

ghost commented Oct 23, 2018

This was already rejected in #20774 and #23248.

@maverigz
Copy link
Author

maverigz commented Oct 23, 2018

@bontibon Okay thanks. I didn't find these when I first researched but I understand now that it is a design-decision to not support conditional operators, although I don't like it. Thanks again, issue closed.

@golang golang locked and limited conversation to collaborators Oct 23, 2019
@bradfitz bradfitz added the error-handling Language & library change proposals that are about error handling. label Oct 29, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
error-handling Language & library change proposals that are about error handling. FrozenDueToAge
Projects
None yet
Development

No branches or pull requests

3 participants