golang 语言数组判断空 2019-8-14 修改 - Go语言中文社区

golang 语言数组判断空 2019-8-14 修改


package main

import "fmt"

func main() {
	var arr []string
	
	if arr == nil {
		fmt.Println("this is null")
	}

	if len(arr) > 0 {
		fmt.Println("len arr > 0")
	}else{
		fmt.Println("len this is null")
	}
	
	if arr[0] != "" {
		fmt.Println("arr 0 != null")	
	}else{
		fmt.Println("[0] this is null")
	}

}

输出是:

原文写错,谢谢 cuikuifa评论了你的博文 指出。我修改成上面的了。

相当然的写成了这样:

if len(sourceArr) > 0 {
    //非空执行

就出错了,空也执行了。

因为数组空时也是长度为1

改变这样

if sourceArr[0] != "" {
    // 非空执行

就成了。

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/wide288/article/details/84936953
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-03-01 20:59:11
  • 阅读 ( 1274 )
  • 分类:Go

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢