GO语言 切片的缩短和增长原理

 1 package main
 2 
 3 import "fmt"
 4 //import  OS   "os"
 5 //import "strings"
 6 //import "path/filepath"
 7 
 8 type Stack []interface{}
 9 
10 func (s *Stack)f() {
11   stack := *s
12 
13   fmt.Printf("%p %d %d\n", &s, len(stack), cap(stack))
14 
15   *s = stack[:len(stack) - 1] //减少
16 
17   fmt.Printf("%p %d %d\n", &s, len(*s), cap(*s))
18 
19   fmt.Printf("%p %d %d\n", stack, len(stack), cap(stack))
20 
21   stack = append(stack, "e", "f") //增加
22 
23   fmt.Printf("%p %d %d\n",stack, len(stack), cap(stack))
24 }
25 
26 
27 func main() {
28   var  s Stack
29   s = append(s, "a", "b", "c", "d")   
30   s.f()
31   fmt.Println(s)
32   fmt.Printf("%t\n", false)
33 }

 

posted on 2013-12-09 11:16  笔记吧... 可能只有自己看得懂  阅读(753)  评论(0编辑  收藏  举报