Go: proposal: strings: add IsEmpty function

Created on 31 Aug 2019  路  5Comments  路  Source: golang/go

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 == ""

FrozenDueToAge Proposal

Most helpful comment

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

All 5 comments

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.

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

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

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.

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

Was this page helpful?
0 / 5 - 0 ratings