"使用Golang判断数组是否为空:简单高效的方法"\n\n可以使用以下代码来判断一个数组是否为空:\n\ngo\npackage main\n\nimport (\n\t\"fmt\"\n)\n\nfunc main() {\n\tarr1 := []int{} // 空数组\n\tarr2 := []int{1, 2, 3} // 非空数组\n\n\tif len(arr1) == 0 {\n\t\tfmt.Println(\"arr1 is empty\")\n\t} else {\n\t\tfmt.Println(\"arr1 is not empty\")\n\t}\n\n\tif len(arr2) == 0 {\n\t\tfmt.Println(\"arr2 is empty\")\n\t} else {\n\t\tfmt.Println(\"arr2 is not empty\")\n\t}\n}\n\n\n输出结果:\n\narr1 is empty\narr2 is not empty\n\n\n使用len(arr)来获取数组的长度,如果长度为0,则说明数组为空。

Golang 判断数组是否为空:简单高效的方法

原文地址: http://www.cveoy.top/t/topic/pMp1 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录