Member-only story
5 Mistakes Iâve Made in Go
To err is human, to forgive divine.
â Alexander Pope
These are mistakes that Iâve made writing Go. Although these might not cause any sort of error but they can potentially affect the software.
1. Inside Loop
There are several ways to make a mess inside a loop that you need to be aware of.
1.1 Using reference to loop iterator variable
Due to efficiency, the loop iterator variable is a single variable that takes different values in each loop iteration. It might lead to unwitting behavior.
The result will be:
Values: 3 3 3
Addresses: 0xc000014188 0xc000014188 0xc000014188
As you can see all elements in out
slice are 3. It is actually easy to explain why this did happen: in every iteration, we append the address of v
to the out
slice. As mentioned before v
is a single variable that takes new values in each iteration. So as you can see in the second line of the output, addresses are theâ¦