Go语言判断切片为空的方法
在 Go 语言中,判断一个切片是否为空,可以使用 len 函数来获取切片的长度,然后判断长度是否为 0。如果长度为 0,则表示切片为空。
示例代码如下:
package main
import "fmt"
func main() {
var s []int
if len(s) == 0 {
fmt.Println('slice is empty')
} else {
fmt.Println('slice is not empty')
}
}
输出结果为:
slice is empty
原文地址: https://www.cveoy.top/t/topic/qiOE 著作权归作者所有。请勿转载和采集!