說我想改變數組中所有對象的值。 我喜歡範圍語法不僅僅是命名爲循環。轉:您可以使用範圍與切片,但獲得參考? (迭代)
所以,我想:
type Account struct {
balance int
}
type AccountList []Account
var accounts AccountList
...
....
// to init balances
for _,a := range(accounts) {
a.balance = 100
}
的是,由於沒有工作距離爲accountList條目的副本,因此,我們只更新副本。
這不會因爲我需要它的工作:
for a := range(accounts) {
accounts[a].balance = 100
}
但是,代碼有內部的一個額外的查找for循環。
有沒有辦法做一個迭代器,獲取對AccountList中的結構的引用?
是否有理由'AccountList`不能是`[] * Account`? – mkb 2011-02-09 18:26:28