请使用golang 判断数组是否为空
可以使用以下代码来判断一个数组是否为空:
package main
import (
"fmt"
)
func main() {
arr1 := []int{} // 空数组
arr2 := []int{1, 2, 3} // 非空数组
if len(arr1) == 0 {
fmt.Println("arr1 is empty")
} else {
fmt.Println("arr1 is not empty")
}
if len(arr2) == 0 {
fmt.Println("arr2 is empty")
} else {
fmt.Println("arr2 is not empty")
}
}
输出结果:
arr1 is empty
arr2 is not empty
使用len(arr)来获取数组的长度,如果长度为0,则说明数组为空。
原文地址: https://www.cveoy.top/t/topic/h3uM 著作权归作者所有。请勿转载和采集!