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

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -