一、
d := append(c, e...) d的长度如果没有超过c的容量,则共享数组。
否则,搬迁。
二、copy值拷贝。
package main
import "fmt"
func Repeat(s string, count int) string {
b := make([]byte, len(s)*count)
bp := copy(b, s)
fmt.Println(bp)
for bp < len(b) {
copy(b[bp:], b[:bp])
bp *= 2
}
return string(b)
}
func test2() {
data := make([]int, 10, 20)
data[0] = 1
data[1] = 2
fmt.Println("length:", len(data), ":", data)
dataappend := make([]int, 10, 20)//len <=10 则 result[0] = 99 会 影响源Slice
dataappend[0] = 1
dataappend[1] = 2
fmt.Println("length:", len(dataappend), ":", dataappend)
result := append(data, dataappend...)
result[0] = 99
result[11] = 98
data = append(data, 88)
fmt.Println("length:", len(data), ":", data)
fmt.Println("length:", len(result), ":", result)
fmt.Println("length:", len(dataappend), ":", dataappend)
}
func main() {
//str := Repeat("_", 50)
//fmt.Println(str)
//var a []int
//a = []int{1, 2, 3, 4}
//var b []int
//b = make([]int, 4)
//copy(b, a)
//b[1] = 999
//fmt.Println(b, a)
//
//
//c := []int{5, 6, 7, 8}
c := make([]int, 4, 6)
c[0] = 5
c[1] = 5
c[2] = 5
c[3] = 5
e := make([]int, 2, 6)
e[0] = 10
e[1] = 11
d := append(c, e...)
d[1] = 888
c[2] = 666
fmt.Println(c, d)
//test2()
}