我试图将切片[1,2]和切片[3,4]结合起来。我如何在围棋中做到这一点?

我试着:

append([]int{1,2}, []int{3,4})

但有:

cannot use []int literal (type []int) as type int in append

然而,文档似乎表明这是可能的,我错过了什么?

slice = append(slice, anotherSlice...)

当前回答

要连接两个切片,

func main() {
    s1 := []int{1, 2, 3}
    s2 := []int{99, 100}
    s1 = append(s1, s2...)

    fmt.Println(s1) // [1 2 3 99 100]
}

将单个值附加到片

func main() {
    s1 :=  []int{1,2,3}
    s1 := append(s1, 4)
    
    fmt.Println(s1) // [1 2 3 4]
}

将多个值追加到一个片

func main() {
    s1 :=  []int{1,2,3}
    s1 = append(s1, 4, 5)
    
    fmt.Println(s1) // [1 2 3 4]
}

其他回答

Appending to and copying slices The variadic function append appends zero or more values x to s of type S, which must be a slice type, and returns the resulting slice, also of type S. The values x are passed to a parameter of type ...T where T is the element type of S and the respective parameter passing rules apply. As a special case, append also accepts a first argument assignable to type []byte with a second argument of string type followed by .... This form appends the bytes of the string. append(s S, x ...T) S // T is the element type of S s0 := []int{0, 0} s1 := append(s0, 2) // append a single element s1 == []int{0, 0, 2} s2 := append(s1, 3, 5, 7) // append multiple elements s2 == []int{0, 0, 2, 3, 5, 7} s3 := append(s2, s0...) // append a slice s3 == []int{0, 0, 2, 3, 5, 7, 0, 0} Passing arguments to ... parameters If f is variadic with final parameter type ...T, then within the function the argument is equivalent to a parameter of type []T. At each call of f, the argument passed to the final parameter is a new slice of type []T whose successive elements are the actual arguments, which all must be assignable to the type T. The length of the slice is therefore the number of arguments bound to the final parameter and may differ for each call site.

你问题的答案是Go编程语言规范中的示例s3:= append(s2, s0…)例如,

s := append([]int{1, 2}, []int{3, 4}...)

在第二片之后加点:

//                           vvv
append([]int{1,2}, []int{3,4}...)

这和其他变进函数一样。

func foo(is ...int) {
    for i := 0; i < len(is); i++ {
        fmt.Println(is[i])
    }
}

func main() {
    foo([]int{9,8,7,6,5}...)
}

Append()函数和展开运算符

可以使用标准golang库中的append方法连接两个片。这类似于变进函数的运算。所以我们需要用…

package main

import (
    "fmt"
)

func main() {
    x := []int{1, 2, 3}
    y := []int{4, 5, 6}
    z := append([]int{}, append(x, y...)...)
    fmt.Println(z)
}

上述代码的输出是:[1 2 3 4 5 6]

并不是反对其他答案,但我发现文档中的简短解释比其中的例子更容易理解:

func append func append(slice []Type, elems ...Type) []Type The append built-in function appends elements to the end of a slice. If it has sufficient capacity, the destination is resliced to accommodate the new elements. If it does not, a new underlying array will be allocated. Append returns the updated slice. It is therefore necessary to store the result of append, often in the variable holding the slice itself: slice = append(slice, elem1, elem2) slice = append(slice, anotherSlice...) As a special case, it is legal to append a string to a byte slice, like this: slice = append([]byte("hello "), "world"...)

似乎是泛型的完美用途(如果使用1.18或更高版本)。

func concat[T any](first []T, second []T) []T {
    n := len(first);
    return append(first[:n:n], second...);
}