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

var s stringif 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教程栏目。

更多相关文章

  1. golang中如何比较字符串是否相等
  2. golang 怎么拼接字符串
  3. golang判断字符是否存在字符串中
  4. golang怎么判断字符串是否为空
  5. golang怎么把字符串转成Int类型
  6. golang怎么拼接字符串数组
  7. golang如何判断字符串是否为空
  8. 常见的 Go 处理字符串的技巧
  9. go语言中一些字符串操作介绍

随机推荐

  1. jquery内部实现原理分析
  2. 用replaceWith()对DOM结点操作时应注意的
  3. JavaScript / jQuery:如何链接使用console
  4. 动态添加表单元素,将它们存储到MySQL并在
  5. Jquery点击加载更多
  6. 美元。针对简单的php, post返回对象
  7. 仅当鼠标停留在元素上时,jQuery才会激活鼠
  8. 如何重新创建中高亮显示功能?
  9. [原]jQuery Tab插件,用于在Tab中显示ifram
  10. 利用jQuery的$.event.fix函数统一浏览器e