golang判断字符串是否为空的方法

golang判断字符串是否为空的方法,第1张

golang判断字符串是否为空的方法

golang判断字符串是否为空的方法:

var s string
if s=="" {
    
}

或:

if len(s)==0{}

推荐使用第一种方法判断字符串是否为空,就字符串是否为空这个语义而言,第一种语义更直接了当,应该是更推荐的。

> 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

更多golang知识请关注golang教程栏目。

以上就是golang判断字符串是否为空的方法的详细内容,

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/683808.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-20
下一篇 2022-04-20

发表评论

登录后才能评论

评论列表(0条)

保存