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

proposal: strings: add IsEmpty function #33991

Closed
mahadevTW opened this issue Aug 30, 2019 · 5 comments
Closed

proposal: strings: add IsEmpty function #33991

mahadevTW opened this issue Aug 30, 2019 · 5 comments

Comments

@mahadevTW
Copy link

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

$ go version
go version go1.12.7 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What did you do?

Try to find better to way to check if the string is empty.

What did you expect to see?

Expecting verbose way to check if the string is empty rather than using,

Len(s) == 0

or

s == ""

@ianlancetaylor ianlancetaylor changed the title Add IsEmpty api in strings package proposal: strings: add IsEmpty function Aug 30, 2019
@gopherbot gopherbot added this to the Proposal milestone Aug 30, 2019
@ianlancetaylor
Copy link
Contributor

The choices seem to be:
`

    len(s) == 0
    s == ""
    strings.IsEmpty(s)

I don't see the advantage of adding another approach that is longer to write and no easier to read.

@mahadevTW
Copy link
Author

@ianlancetaylor
isn't this more obvious, cleaner and readable way.
it removes magic numbers/string constants from code which makes cleaner.

@ianlancetaylor
Copy link
Contributor

Constants like "" and 0 are not magic. They are ordinary language values that every Go programmer understands.

@robpike
Copy link
Contributor

robpike commented Aug 31, 2019

What approach would you take to check if an integer is zero? Surely you would just write

   i == 0

and not expect a function IsZero? Well, strings are values, just like integers, and the way to check if they have a particular value is to compare them against that value:

  s == ""

Strings also have the added, more "magic" as you put it, mechanism of checking their length. Surely a third way is not required.

@mahadevTW
Copy link
Author

most of the people more inclined towards an existing way, I am closing this issue.
Thank you for inputs here.

@golang golang locked and limited conversation to collaborators Aug 30, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants