go实现切片任意位置插入

package main

import "fmt"

func main() {
	people := [][2]int{
		{7, 0}, {4, 4}, {27, 1}, {5, 0}, {6, 1}, {5, 2},
	}
	//把{27, 1}插到第一个位置
	p := people[2] // 先复制一份
	copy(people[1:3], people[0:3])
	people[0] = p
	fmt.Println(people)

	//把{27, 1}复制到最后一个位置,第一个位置同理
	//p := people[2]
	//copy(people[2:], people[3:])
	//people[len(people)-1] = p
	//fmt.Println(people)
}
posted @ 2020-11-26 15:54  devhg  阅读(1116)  评论(0编辑  收藏  举报