go语言怎么判断字符串是否为空

发布时间:2020-06-12 19:44:39 作者:鸽子
来源:亿速云 阅读:6219

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判断字符串是否为空的方法的详细内容,更多请关注亿速云其它相关文章!

推荐阅读:
  1. java 判断字符串是否为空
  2. golang怎么判断map是否为空

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

golang go语言 字符串判断

上一篇:安装linux中安装包的方法

下一篇:WIN2012 TCP ECN 启用导致速度慢

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》