- nil
var ss []student
- length is zero
dd := []student{}
- ff[0] is {"",0}, length is 1
ff := []student{{}}
package main
import (
"fmt"
)
func main() {
// ss: ss is nil
var ss []student
// dd's length is zero
dd := []student{}
// ff[0] is {"",0}
ff := []student{{}}
if ss == nil {
fmt.Printf("ss is nil
")
} else {
fmt.Printf("ss is not nil
")
}
if dd == nil {
fmt.Printf("dd is nil
")
} else if len(dd) == 0 {
fmt.Printf("dd's length is zero
")
} else {
fmt.Printf("dd[0] is %v, length is %d
", dd[0], len(dd))
}
if ff == nil {
fmt.Printf("ff is nil
")
} else if len(ff) == 0 {
fmt.Printf("ff's length is zero
")
} else {
fmt.Printf("ff[0] is %v, length is %d
", ff[0], len(ff))
}
}
type student struct {
name string
class int
}
➜ go run nil_slice/main.go
ss is nil
dd's length is zero
ff[0] is { 0}, length is 1