Friday, June 22, 2018

I'm new to Golang development, and there are lots of things I consider weird with the language. I've just discovered this gotcha, luckily before any code went live, so I figured I'd post about it.

Variables of the same name can be declared at multiple scopes within the same function, and as functions can return tuples, it's possible that, when declaring and initialising variables at the same time, you intend to actually reuse a previously declared variable.

By separately declaring z before initialisation, z and x can both be assigned a value separately to declaration.

I know this will come across as pretty simplistic, and is obvious when you think about it, but I still think it would be really easy to increase some subtle bugs, which even careful eyes might not spot, because of it.