go切片的Add与Del

func Add(s []int, index int, value int) []int {
	len := len(s)
	if 0 > index || len <= index {
		panic("error")
	}
	s = append(s, s[len - 1])
	temp := len - index
	for temp > 0 {
		if len - 2 >= 0 {
			s[len - 1] = s[len - 2]
		}
		temp--
		len--
	}
	s[index] = value
	return s
}

func Del(s []int, index int) []int {
	len := len(s)
	if 0 > index || index > len {
		panic("error")
	}

	temp := len - index - 1
	for temp > 0 {
		s[index] = s[index + 1]
		index++
		temp--
	}
	s1 := s[0:len - 1]
	return s1
}

  

go切片的Add与Del

上一篇:标识符命名规则


下一篇:vue组件传值eventBus