码迷,mamicode.com
首页 > 其他好文 > 详细

go 切片的 插入、删除

时间:2017-06-28 17:12:28      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:code   port   art   import   logs   append   gsl   func   make   

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 切片的 插入、删除

标签:code   port   art   import   logs   append   gsl   func   make   

原文地址:http://www.cnblogs.com/chenchao1990/p/7090384.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!