Home >Backend Development >Golang >Golang method to determine whether a string is empty

Golang method to determine whether a string is empty

尚
Original
2019-12-26 09:26:0326724browse

Golang method to determine whether a string is empty

Golang's method to determine whether a string is empty:

var s string
if s=="" {
    
}

or:

if len(s)==0{}

It is recommended to use the first method to determine whether a string is empty Whether the string is empty, in terms of the semantics of whether the string is empty, the first semantics is more straightforward and should be more recommended.

> The one that makes the code clear.
If I'm about to look at element x I typically write
len(s) > x, even for x = = 0, but if I care about
"is it this specific string" I tend to write s == "".
>
>It's reasonable to assume that a mature compiler will compile
len(s) == 0 and s == "" into the same, efficient code.
Right now 6g etc do compile s == "" into a function call
while len(s) == 0 is not, but that's been on my to-do list to fix.

>Make the code clear.
>
>Russ

More For golang knowledge, please pay attention to the golang tutorial column.

The above is the detailed content of Golang method to determine whether a string is empty. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn