GO数组扩展

I was testing some go array initialization and general usage but I can not figure out why can not expand an array initialized with a defined length?

package main

func main() {
    arr0 := []int{1, 2, 3}  // 
    add(arr0...)            // OK

    arr1 := [3]int{1, 2, 3} // 
    slice := arr1[:]        //
    add(slice...)           // OK

    arr2 := [3]int{}        // 
    arr2[0] = 1             //
    arr2[1] = 2             //
    arr2[3] = 3             //
    add(arr2...)            // cannot use arr2 (type [3]int) as type 
                            // []int in argument to add
 }

func add(terms ...int) (sum int) {
    for _, term := range terms {
       sum += term
    }
    return
}

As other said:

[n]type - Array (fixed size)

[]type - Slice

A quick solution for your case would be a simple conversion from array to slice.

add(arr2[:]...)

That will just make it a slice pointing to the storage of arr2 (not a copy)

when use the operator ... as a tail, such as T.... T should always be slice.