Take this incredibly simple example. which shows variable assignment in and out of a block.
On compilation this results in: u declared and not used
var u string
{
u, err := url.Parse("http://bing.com/search?q=dotnet")
if err != nil {
log.Fatal(err)
}
}
log.Debug(u)
This simulates a logic block during which we might assess several things and set a var to the value we like depending on logic evaluation. How is this possible?
udeclared inside the block is not used (the actual error will tell you the exact line). There's no magic here, and the error is probably saving you from a bug in your code.uanderrvariables, becauseuis not declared in the same block. Redeclaration can only happen if the variable was declared in the same block.u, err :=is a declare and assign of bothuanderrinside a block, masking thevar u stringdeclared in a higher scope.