The problem is that using the first for / range loop, you get the value of struct in variable a . The second for the loop / range that you used solves the problem by directly accessing the memory in the slice.
However, you are incorrect in stating that in the second cycle there is an βadditionalβ search for the second cycle. The loop condition simply checks the cut length and increments the counter until it reaches the end. Then the accounts[a] expression will actually search for the array and directly manipulate the memory. If something, the second for the loop translates into fewer instructions, because it does not copy the contents of the structural value to the variable first.
I think you are worried about having to refer to accounts[i] every time. If you want to perform several manipulations with the account inside the for loop, I think the best way to solve it:
for i := range accounts { a := &accounts[i] a.balance = 100
Another possible solution, as Mew suggested, is simply to have slice retention indicators. There are advantages to any of the ways to do this, but the dilemma is the same regardless of whether you are in C or Go. Go just has a little more syntactic sugar.
source share