首页 > 代码库 > go 切片的 插入、删除
go 切片的 插入、删除
package main
import (
"fmt"
)
func InsertSpringSliceCopy(slice, insertion []string, index int) []string {
result :=make([]string, len(slice)+len(insertion))
at := copy(result, slice[:index])
at += copy(result[at:], insertion)
copy(result[at:], slice[index:])
return result
}
func InsertSpringSlice(slice, insertion []string, index int) []string {
return append(slice[:index], append(insertion, slice[index:]...)...)
}
func RemoveSpringSliceCopy(slice []string, start,end int) []string {
result := make([]string, len(slice)-(end-start))
at :=copy(result, slice[:start])
copy(result[at:], slice[end:])
return result
}
func RemoveSpringSlice(slice []string, start, end int) []string {
return append(slice[:start], slice[end:]...)
}
func main() {
s := []string{"C", "H", "A", "O"}
x := InsertSpringSliceCopy(s, []string{"a", "b", "c"}, 0)
fmt.Println(x)
w := []string {"C", "H", "E", "N"}
ww :=InsertSpringSlice(w, []string{"c", "h", "a", "o"}, len(w))
fmt.Println(ww)
d := []string {"a", "b", "c", "d", "e", "f"}
dd := RemoveSpringSliceCopy(d, 2, 4)
fmt.Println(dd)
d2 := []string {"A", "B", "C", "D", "E", "F", "G"}
dd2 := RemoveSpringSlice(d2, 2, 4)
fmt.Println(dd2)
}
go 切片的 插入、删除
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。