go语言判断数组是否为空的方法

go语言数组是类型相同的元素的集合。例如,整数 5, 8, 9, 79, 76 的集合就构成了一个数组。Go不允许在数组中混合使用不同类型的元素(比如整数和字符串)。

golang判断数组是否为空:

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")
	}
 
}

在go语言中nil是一个经常使用的,重要的预先定义好的标识符。它是许多中类型的零值表示。

len() 可以用来查看数组或slice的长度。

以上就是golang怎么判断数组是否为空的详细内容,更多请关注其它相关文章!