golang-切片copy
package main import ( "fmt" ) func main() { fmt.Println("-------------") data := []int{1,2,3,5,3,6,2,3} s1 := data[6:]//23 s2 := data[:3]//123 fmt.Println(s1) fmt.Println(s2) copy(s2,s1)//目标切片,源切片,对应位置拷贝 fmt.Println(s2)//[2 3 3] } /* ------------- [2 3] [1 2 3] [2 3 3] */
package main import ( "fmt" ) func remove(data []int, idx int) []int { copy(data[idx:], data[idx+1:])//目标地址,源地址 return data[:len(data)-1] } func main() { data := []int{5, 6, 7, 8, 9} // {5, 6, 8, 9, 9} fmt.Println(data[3:]) afterData := remove(data, 2) fmt.Println("afterData:", afterData) } /* [8 9] afterData: [5 6 8 9] */
网名:浩秦;
邮箱:root#landv.pw;
博客:landv.cnblogs.com;
只要我能控制一個國家的貨幣發行,我不在乎誰制定法律。金錢一旦作響,壞話隨之戛然而止。