0
具有切片和接受作爲參數的切片指針的函數。如何使用指向該切片的指針獲取切片項目
mainSlice := []int{0,8,5,4,6,9,7,1,2,3,6,4,5,7}
doSmthWithSlice(mainSlice)
有什麼方法來獲得使用指針片切片項目,但沒有複製的價值指針點到新片?
func doSmthWithSlice(slcPtr *[]int) {
*slcPtr[3] = 777 // this does NOT works, because *[]int does not support indexing
// Don't want to implement it
// like this
newSlice := *slcPtr
newSlice[3] = 777
*slcPtr = newSlice
}
謝謝
附:
不好意思問這種原始問題。我是新中去
你只是打敗了我! https://play.golang.org/p/x3fQXtjcnO –