0
我有一個結構轉到函數指針的問題
type keeper struct {
ptr int32
}
然後我添加了一個功能,它
func(l keeper) next() {
l.ptr++
}
但是,當我創建一個新門將,調用next()
tester := keeper {
ptr: 0,
}
test.next()
看來我沒有修改測試器內的ptr值。如果我改變功能是一個指針,它然後工作
func(l *keeper) next() {
l.ptr++
}
爲什麼這樣?
查看另一個相關的問答:[https://stackoverflow.com/questions/27775376/value-receiver-vs-pointer-receiver-in-golang](https://stackoverflow.com/questions/27775376/value-接收機-VS-指針接收器式golang) – putu