第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么我們在這個追加函數(shù)調用中需要三個點?

為什么我們在這個追加函數(shù)調用中需要三個點?

Go
炎炎設計 2022-08-24 17:28:05
我正在學習一個教程來學習Go中的Rest API。有這段代碼。func removeBook(w http.ResponseWriter, r *http.Request) {    params := mux.Vars(r)    id, _ := strconv.Atoi(params["id"])    for i, item := range books {        if item.ID == id {            books = append(books[:i], books[i+1:]...)        }    }}特別是這部分。books = append(books[:i], books[i+1:]...)OK,將一個或多個項追加到切片(在本例中為結構切片)。 獲取從開始到 的所有內容,不包括 。然后表示后面的下一項,在右側添加冒號表示從那里到結束的所有內容。因此,這意味著我們將所有項目都保留到排除,然后在 之后獲取所有項目。這意味著我們正在排除/刪除 ,這就是功能點。appendbooksbookbooks[:i]iii+1iiiiiremoveBook我有這么多的邏輯,但是這三個點在那里做什么?我知道可變參數(shù)函數(shù)中使用了三個點,但是為什么在這個追加函數(shù)中我們需要三個點呢?當我刪除這3個點時,我的編輯說“不能使用books[i + 1:](類型[]Book的值)作為要追加的參數(shù)中的Book值”。如果您想查看完整的代碼,請在此要點中。
查看完整描述

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])


查看完整回答
反對 回復 2022-08-24
?
慕哥6287543

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

因此,您只能追加元素,而不能追加整個切片。


查看完整回答
反對 回復 2022-08-24
  • 2 回答
  • 0 關注
  • 93 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號