1 回答

TA貢獻1871條經(jīng)驗 獲得超8個贊
args[0] = &s2
這在 之外沒有任何作用MockScan。您正在更改args[0]指向的內(nèi)容,而不是更改指向的內(nèi)容args[0]。
你想做類似的事情
func MockScan(args ...interface{}) {
// In real life, there would be some logic here to figure out what
// kind of pointer we're dealing with, or some error handling to
// handle the case where the user didn't pass a *string
dst := args[0].(*string)
*dst = "A new value"
}
func main() {
var s1 string = "An old value"
fmt.Println(s1)
MockScan(&s1)
fmt.Println(s1)
}
請注意,如果我們在&s1調(diào)用之前和之后在 main 中打印,它仍然不會改變。它仍然是相同的變量,仍然位于相同的地址。我們已將一個指針傳遞&s1給MockScan,以便MockScan可以使用該指針將值寫入s1。
- 1 回答
- 0 關注
- 105 瀏覽
添加回答
舉報