1 回答

TA貢獻(xiàn)1843條經(jīng)驗(yàn) 獲得超7個(gè)贊
問題是您返回的是元素的副本,而不是數(shù)組中元素的實(shí)際地址
return &acc
如下更改您的查找功能
func find(accounts []Account, username string) *Account {
for idx , acc := range accounts {
if acc.IsActive && acc.Username == username {
return &accounts[idx]
}
}
return nil
}
你會(huì)得到預(yù)期的回應(yīng)
[{1 name address city email@email.com 123456789 username 1234 true}]
&{1 name address city email@email.com 123456789 username 1234 true}
[{1 updated address city email@email.com 123456789 username 1234 true}]
&{1 updated address city email@email.com 123456789 username 1234 true}
您可以在 go playground中查看更新的代碼
- 1 回答
- 0 關(guān)注
- 102 瀏覽
添加回答
舉報(bào)