2 回答

TA貢獻2016條經(jīng)驗 獲得超9個贊
在此處檢查追加定義。
func append(slice []Type, elems ...Type) []Type
在第二個論點中,它接受多個“elems”。
在您給出的示例中,它實質上是將元素作為逗號分隔的參數(shù)列表進行傳遞。
假設書的長度是10,而我是7。然后,它傳遞切片中的前 7 個元素(項 0 到 6),然后作為逗號分隔的列表傳遞第 8 個和第 9 個元素。
books = append(books[:i], books[i+1:]...)
其實是
books = append(books[:7], books[8], books[9])

TA貢獻1831條經(jīng)驗 獲得超10個贊
// 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"...)
func append(slice []Type, elems ...Type) []Type
因此,您只能追加元素,而不能追加整個切片。
- 2 回答
- 0 關注
- 93 瀏覽
添加回答
舉報