We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
I strongly think that a ternary operator is needed on Go
Here is why
I have a struct
type VerificationStatus struct { StatusCode int StatusMessage string StatusData interface{} }
I wish to send that struct based on a json marshal like below
data, err := json.Marshal(struct { requestId string }{ requestId: string(responseData), })
Now to send it back this is the long code I have to write
if err != nil { return VerificationStatus{ StatusCode: http.StatusConflict, StatusMessage: "Verification Failed", } } else { return VerificationStatus{ StatusCode: http.StatusOK, StatusMessage: "Verification Started", StatusData: data, } }
If we had a ternary operator I could have just done this
return VerificationStatus{ StatusCode: http.StatusOK, StatusMessage: "Verification Started", StatusData: err==nil ? data : err.Error(), }
Thanks for this amazing language though.
The text was updated successfully, but these errors were encountered:
This has been discussed before: #33171
If you think you have something new to add, please file a new proposal while filling the form at https://github.com/golang/proposal/blob/master/go2-language-changes.md.
Sorry, something went wrong.
Note that the long code doesnt do the same then the proposed short form as StatusCode and StatusMessage are changed.
return VerificationStatus{ StatusCode: err==nil ? http.StatusOK : http.StatusConflict StatusMessage: err==nil ? "Verification Started" : "Verification Failed" StatusData: err==nil ? data : err.Error(), }
starts to look much more complex to me to understand what err vs non err does then the version that uses one if.
if
No branches or pull requests
I strongly think that a ternary operator is needed on Go
Here is why
I have a struct
I wish to send that struct based on a json marshal like below
Now to send it back this is the long code I have to write
If we had a ternary operator I could have just done this
Thanks for this amazing language though.
The text was updated successfully, but these errors were encountered: