34 lines
423 B
Go
34 lines
423 B
Go
![]() |
package main
|
||
|
|
||
|
import "fmt"
|
||
|
|
||
|
func printSlice(s []int) {
|
||
|
fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s)
|
||
|
}
|
||
|
|
||
|
func main() {
|
||
|
s := []int{2, 3, 5, 7, 11, 13}
|
||
|
printSlice(s)
|
||
|
|
||
|
s = s[:0]
|
||
|
printSlice(s)
|
||
|
|
||
|
s = s[:4]
|
||
|
printSlice(s)
|
||
|
|
||
|
s = s[2:]
|
||
|
printSlice(s)
|
||
|
|
||
|
// Extend beyond capacity
|
||
|
// s = s[:10]
|
||
|
// printSlice(s)
|
||
|
|
||
|
// Zero value of a slice is nil
|
||
|
var ns []int
|
||
|
printSlice(ns)
|
||
|
|
||
|
if ns == nil {
|
||
|
fmt.Println("nil!!")
|
||
|
}
|
||
|
}
|