Home >Backend Development >Golang >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!