The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +772K followers.

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…

--

--

The Startup
The Startup

Published in The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +772K followers.

Ali Josie
Ali Josie

Written by Ali Josie

Software Engineer & Information Security Enthusiast

Responses (2)