如何将 int 添加到 slice

我有一个我使用创建的切片


var x []int;

for i := 2; i < 10; i += 2 {

    x = append(x, i);

}

我想在这个切片前加上一个整数,比如


x = append(2, x)

但显然它不会工作,因为 append 需要一个切片作为第一个参数。


我试过这个,但它只适用于字符串,在我的情况下不起作用。



杨__羊羊
浏览 152回答 4
4回答

杨魅力

使用切片复合文字: []int{1},例如:package mainimport (    "fmt")func main() {    var x []int    for i := 2; i < 10; i += 2 {        x = append(x, i)    }    fmt.Println(x)    x = append([]int{1}, x...)    fmt.Println(x)}游乐场:https://play.golang.org/p/Yc87gO7gJlD输出:[2 4 6 8] [1 2 4 6 8]然而,这个更高效的版本可能会进行更少的分配,只有在没有空闲切片容量时才需要分配。package mainimport (    "fmt")func main() {    var x []int    for i := 2; i < 10; i += 2 {        x = append(x, i)    }    fmt.Println(x)    x = append(x, 0)    copy(x[1:], x)    x[0] = 1    fmt.Println(x)}游乐场: https: //play.golang.org/p/fswXul_YfvD输出:[2 4 6 8][1 2 4 6 8]好的代码必须是可读的。在 Go 中,我们经常将实现细节隐藏在函数中。Go 编译器正在优化编译器,prependInt内联小而简单的函数(如 )。package mainimport (    "fmt")func prependInt(x []int, y int) []int {    x = append(x, 0)    copy(x[1:], x)    x[0] = y    return x}func main() {    var x []int    for i := 2; i < 10; i += 2 {        x = append(x, i)    }    fmt.Println(len(x), cap(x), x)    x = prependInt(x, 1)    fmt.Println(len(x), cap(x), x)}游乐场: https: //play.golang.org/p/wl6gvoXraKH输出:4 4 [2 4 6 8]5 8 [1 2 4 6 8]

慕容森

当前版本是go1.14.11没有 for 循环的前置:package mainimport "fmt"func main () {&nbsp; data := []int{1, 2, 3, 4}&nbsp; fmt.Println(data)&nbsp; data = append([]int{99}, data...)&nbsp; fmt.Println(data)}示例形式:https://codingair.wordpress.com/2014/07/18/go-appendprepend-item-into-slice/使用整数:https://play.golang.org/p/gaLhB5_d1Iu

湖上湖

我知道这个问题只是为了int但我想我会把这个添加到讨论中:https://play.golang.com/p/PNax2a1rL3qimport (    "golang.org/x/exp/constraints")type Number interface {    constraints.Signed | constraints.Unsigned | constraints.Float}func prepend[T Number](list []T, elems ...T) []T {    results := make([]T, len(list)+len(elems))    results = append(results, 0)    copy(results[len(elems):], list)    copy(results[:len(elems)], elems)    return results}

泛舟湖上清波郎朗

另一个答案使用appendand ,但您可以在一两行中copy仅使用 , 来完成:appendpackage mainimport "fmt"func main() {&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; a := []int{20, 30}&nbsp; &nbsp; &nbsp; a = append(append(a, 10), a...)[len(a):]&nbsp; &nbsp; &nbsp; fmt.Println(a)&nbsp; &nbsp;}&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; a := []int{20, 30}&nbsp; &nbsp; &nbsp; a = append(make([]int, 1), a...)&nbsp; &nbsp; &nbsp; a[0] = 10&nbsp; &nbsp; &nbsp; fmt.Println(a)&nbsp; &nbsp;}&nbsp; &nbsp;{ // if starting with empty slice, use: a := make([]int, 0, 1)&nbsp; &nbsp; &nbsp; a := []int{20, 30}&nbsp; &nbsp; &nbsp; a = append(a[:1], a...)&nbsp; &nbsp; &nbsp; a[0] = 10&nbsp; &nbsp; &nbsp; fmt.Println(a)&nbsp; &nbsp;}}或者作为另一种选择,您可以使用链表:package mainimport (&nbsp; &nbsp;"container/list"&nbsp; &nbsp;"fmt")func main() {&nbsp; &nbsp;a := list.New()&nbsp; &nbsp;a.PushBack(20)&nbsp; &nbsp;a.PushBack(30)&nbsp; &nbsp;a.PushFront(10)&nbsp; &nbsp;for n := a.Front(); n != nil; n = n.Next() {&nbsp; &nbsp; &nbsp; fmt.Println(n.Value)&nbsp; &nbsp;}}https://golang.org/pkg/container/list
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go