Go: Can you use range with a slice but get references? (iteration) -
say want change value objects in array. range syntax lot more named loops.
so tried:
type account struct { balance int } type accountlist []account var accounts accountlist ... .... // init balances _,a := range( accounts ) { a.balance = 100 }
that did not work since copy of entries accountlist , updating copy only.
this work need to:
for := range( accounts ) { accounts[a].balance = 100 }
but code has lookup inside loop.
is there way iterator gets references structs in accountlist?
just let accountlist []*account. you'll pointers each account inside range.
Comments
Post a Comment