首页 >后端开发 >Golang > 正文

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

原创2019-12-26 09:26:03011303

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

php中文网最新课程二维码

声明:本文原创发布php中文网,转载请注明出处,感谢您的尊重!如有疑问,请联系admin@php.cn处理

  • 相关标签:golang
  • 相关文章

    相关视频


    网友评论

    文明上网理性发言,请遵守 新闻评论服务协议

    我要评论
  • 专题推荐

    推荐视频教程
  • Go语言教程手册Go语言教程手册
  • Go参考手册Go参考手册
  • Go 中文开发手册Go 中文开发手册
  • go语言基础与基本函数go语言基础与基本函数
  • 视频教程分类