package mainimport "fmt"func main(){
var a []intb:=make([]int,0)if a==nil{
fmt.Println("a is nil")}else{
fmt.Println("a is not nil")}if b==nil{
fmt.Println("b is nil")}else{
fmt.Println("b is not nil")}
}
运行结果:
a is nil
b is not nil
Process finished with exit code 0
因此:
通过var a []int创建的切片是一个nil切片
通过b:=make([]int,0)创建的是一个空切片,(底层数组为空,但底层数组指针非空)