码迷,mamicode.com
首页 > 其他好文 > 详细

【玩转Golang】切片的操作

时间:2015-01-20 07:48:02      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:

一、一般操作

二、初始大小和容量

三、改变自身之传址操作

1

func main() {
    var sa []string
fmt.Printf("addr:%p \t\tlen:%v content:%v\n",sa,len(sa),sa);
    for i:=0;i<10;i++{
        sa=append(sa,fmt.Sprintf("%v",i))
        fmt.Printf("addr:%p \t\tlen:%v content:%v\n",sa,len(sa),sa);
    }
    fmt.Printf("addr:%p \t\tlen:%v content:%v\n",sa,len(sa),sa);

}
addr:0x0         len:0 content:[]
addr:0x1030e0c8         len:1 content:[0]
addr:0x10328120         len:2 content:[0 1]
addr:0x10322180         len:3 content:[0 1 2]
addr:0x10322180         len:4 content:[0 1 2 3]
addr:0x10342080         len:5 content:[0 1 2 3 4]
addr:0x10342080         len:6 content:[0 1 2 3 4 5]
addr:0x10342080         len:7 content:[0 1 2 3 4 5 6]
addr:0x10342080         len:8 content:[0 1 2 3 4 5 6 7]
addr:0x10324a00         len:9 content:[0 1 2 3 4 5 6 7 8]
addr:0x10324a00         len:10 content:[0 1 2 3 4 5 6 7 8 9]
addr:0x10324a00         len:10 content:[0 1 2 3 4 5 6 7 8 9]

 

 

 

func main() {
    var sa = make ([]string,0,10);
fmt.Printf("addr:%p \t\tlen:%v content:%v\n",sa,len(sa),sa);
    for i:=0;i<10;i++{
        sa=append(sa,fmt.Sprintf("%v",i))
        fmt.Printf("addr:%p \t\tlen:%v content:%v\n",sa,len(sa),sa);
    }
    fmt.Printf("addr:%p \t\tlen:%v content:%v\n",sa,len(sa),sa);

}
addr:0x10304140         len:0 content:[]
addr:0x10304140         len:1 content:[0]
addr:0x10304140         len:2 content:[0 1]
addr:0x10304140         len:3 content:[0 1 2]
addr:0x10304140         len:4 content:[0 1 2 3]
addr:0x10304140         len:5 content:[0 1 2 3 4]
addr:0x10304140         len:6 content:[0 1 2 3 4 5]
addr:0x10304140         len:7 content:[0 1 2 3 4 5 6]
addr:0x10304140         len:8 content:[0 1 2 3 4 5 6 7]
addr:0x10304140         len:9 content:[0 1 2 3 4 5 6 7 8]
addr:0x10304140         len:10 content:[0 1 2 3 4 5 6 7 8 9]
addr:0x10304140         len:10 content:[0 1 2 3 4 5 6 7 8 9]

 

 

 

 

func main() {
    var osa = make ([]string,0);
    sa:=&osa;
    for i:=0;i<10;i++{
        *sa=append(*sa,fmt.Sprintf("%v",i))
        fmt.Printf("addr:%p \t content:%v\n",sa,sa);
    }
    fmt.Printf("addr:%p \t content:%v\n",sa,sa);
   
}
addr:0x10328000      content:&[0]
addr:0x10328000      content:&[0 1]
addr:0x10328000      content:&[0 1 2]
addr:0x10328000      content:&[0 1 2 3]
addr:0x10328000      content:&[0 1 2 3 4]
addr:0x10328000      content:&[0 1 2 3 4 5]
addr:0x10328000      content:&[0 1 2 3 4 5 6]
addr:0x10328000      content:&[0 1 2 3 4 5 6 7]
addr:0x10328000      content:&[0 1 2 3 4 5 6 7 8]
addr:0x10328000      content:&[0 1 2 3 4 5 6 7 8 9]
addr:0x10328000      content:&[0 1 2 3 4 5 6 7 8 9]

 

【玩转Golang】切片的操作

标签:

原文地址:http://www.cnblogs.com/dajianshi/p/4235142.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!