Some related posts I've found:
- go variable scope and shadowing
- Golang: variable scope inside if statements
- Limit the scope of variables storing error
Also there are many use cases to Variable Scoping and Shadowing.
Any code samples or answers will be appreciated.
Variable scoping and shadowing:
Advantages:
Different forms of Variable shadowing in Go:
Golang way to limit variable scope (using short-hand assignment inside statements):
When "we need more alphabets", this is nice way to limit variables scope.
Also this works well when you need more local variables or scope:
using
{
and}
pair:Pros: no need to extra statements like if, for, …
Another way to limit variable scope is using function calls:
Pros: scope limit, input value type parameters are usable like local variables,
Cons: call/return time, and stack usage: if it is not optimized by compiler
Another way is shadowing global variables:
See: Variable shadowing and Scope.
And: Declarations and scope: