go的slice拷贝,修改,append
package main
import (
"fmt"
)
func testMake1() {
var a []int
//a = make([]int, 1, 10) // 切片如果直接使用a = append(a, 11)可以不初始化, 直接a[0] = 10这样赋值,必须初始化
fmt.Printf("00 a=%v\n", a)
fmt.Printf("00 a=%v addr:%p len:%d cap:%d \n", a, a, len(a), cap(a))
//a[0] = 10
fmt.Printf("01 a=%v\n", a)
// a[1] =20
a = append(a, 11)
fmt.Printf("02 a=%v\n", a)
fmt.Printf("03 a=%v addr:%p len:%d cap:%d \n", a, a, len(a), cap(a))
for i := 0; i < 8; i++ {
a = append(a, i)
fmt.Printf("04 a=%v addr:%p len:%d cap:%d \n", a, a, len(a), cap(a))
}
a = append(a, 100)
fmt.Printf("05 扩容之后的地址 a=%v addr:%p len:%d cap:%d \n", a, a, len(a), cap(a))
}
func testMake2() {
var a []int
a = make([]int, 5, 10)
a = append(a, 10)
fmt.Printf("a=%v \n", a)
b := make([]int, 0, 10)
fmt.Printf("00>>b=%v len:%d cap:%d\n", b, len(b), cap(b))
b = append(a, 100)
fmt.Printf("01>>b=%v len:%d cap:%d\n", b, len(b), cap(b))
}
func testCap() {
a := [...]string{"a", "b", "c", "d", "e", "f", "g", "h"}
b := a[1:3]
fmt.Printf("b=%v len:%d cap:%d\n", b, len(b), cap(b))
b = b[:cap(b)]
fmt.Printf("after reslice>> b:%v len:%d cap:%d\n", b, len(b), cap(b))
}
func testSlice() {
var a []int
fmt.Printf("addr(a)=%p, len:%d, cap:%d\n", a, len(a), cap(a))
if a == nil {
fmt.Printf("a is nil\n")
}
a = append(a, 100)
fmt.Printf("%p, len:%d cap:%d\n", a, len(a), cap(a))
a = append(a, 200)
fmt.Printf("%p, len:%d cap:%d\n", a, len(a), cap(a))
a = append(a, 301)
fmt.Printf("%p, len:%d cap:%d\n", a, len(a), cap(a))
a = append(a, 401)
fmt.Printf("%p, len:%d cap:%d\n", a, len(a), cap(a))
a = append(a, 501)
fmt.Printf("%p, len:%d cap:%d\n", a, len(a), cap(a))
}
func testAppend() {
var a []int = []int{1, 3, 4}
fmt.Printf("01 a:%v \n", a)
var b []int = []int{4, 5, 6}
a = append(a, 23, 34, 45)
fmt.Printf("02 a:%v \n", a)
c := append(a, b...) // 切片后面加...把切片展开
fmt.Printf("03 a:%v \n", a)
fmt.Printf("03 b:%v \n", b)
fmt.Printf("03 c:%v \n", c)
}
func sumArray(a []int) int {
var sum int = 0
for _, v := range a {
sum = sum + v
}
return sum
}
func testSumArray() {
var a [10]int = [10]int{1, 2, 3, 4, 5, 6, 7, 8, 9}
fmt.Printf("Array(a)>>%#v, Slice(a)>>%#v \n", a,a[:])
sum := sumArray(a[:])
fmt.Println("sum>>a:", sum)
}
func modifySlice(a []int) {
a[0] = 100
}
func testModifyCopy() {
var a []int = []int{1, 2, 3, 7, 8, 9, 8, 99}
var b []int = []int{4, 5, 6, 66}
copy(a, b) // 把b中的拷贝到a中去
fmt.Println("a", a)
fmt.Println("b", b)
fmt.Printf("a的类型%T\n", a)
fmt.Printf("b的类型%T\n", b)
}
func testModifySlice() {
var a [3]int = [3]int{1, 2, 3}
fmt.Println("modify before",a)
modifySlice(a[:])
fmt.Println("modify after",a)
}
func main() {
//testMake1()
//testMake2()
//testCap()
//testSlice()
testAppend()
testSumArray()
testModifySlice()
testModifyCopy()
}
案例二:
package main
import(
"fmt"
)
func main(){
var a []string = make([]string,5,10)
fmt.Println("a",a)
for i:=0;i<10;i++{
a=append(a,fmt.Sprintf("%d",i))
}
fmt.Printf("a:%v %d %d",a,len(a),cap(a))
}
切记: 用make(make([]string,5,10))初始化切片时,直接生成了一个切片,他的前5个元素是'',长度为5, 容量10,往他里面添加元素请用a[i],而不是append
输出:
a [ ]
a:[ 0 1 2 3 4 5 6 7 8 9] 15 20
写入自己的博客中才能记得长久