切片操作

2020-03-26  本文已影响0人  NowhereToRun

JS写多了各种数组的操作已经很熟悉,到Golang里面... 啥方法也没有只能自己手撸了...

简单先自己记录一下 修改id对应数组中位置的函数 平时golang写得少 总是忘。。。
类型写死了,并不通用

func AddToTargetPos(id int64, position int32) error {
    // oldOrderList 就先不管从哪来了 

    newList := make([]int64, 0)

    for _, _id := range oldOrderList {
        if id != _id {
            newList = append(newList, _id)
        }
    }

    if position == 0 {
        newList = append([]int64{id}, newList...)
    } else if int(position+1) >= len(oldOrderList) {
        newList = append(newList, id)
    } else {
        rear := append([]int64{}, newList[position:]...)
        newList = append(newList[0:position], id)
        newList = append(newList, rear...)
    }

        // 后续跟的有对 newList的操作  也懒得返回了  就这样吧
    return nil
}

上一篇 下一篇

猜你喜欢

热点阅读