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


本文摘自php中文网,作者尚,侵删。

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

1

2

3

4

var s string

if s=="" {

     

}

或:

1

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判断字符串是否为空的方法的详细内容,更多文章请关注木庄网络博客!!

相关阅读 >>

golang 默认几核运行

go-carbon1.2.0发布了!完善优化对orm的多场景支持

详解golang中方法的receiver为指针和不为指针的区别

golang 面向对象编程

go 字符串常用的系统函数

总结air在go的其他版本上运行可能遇到的问题

golang中main中panic和后续panic处理,以及新开协程的影响

关于golang select典型用法

golang如何判断字符串是否为空

golang中的联合体

更多相关阅读请进入《golang》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...