FAQ

On Tue, Feb 19, 2013 at 9:50 PM, Johann Höchtl wrote:

Sure, but in this case no shadowing happens. But take a look at this case:


var found bool
for idx := range []int{1, 3, 5} {
if found := answertoeverything(idx); found == true {
answer = "We got an answer"
break
}
}

if found {
fmt.Println("The answer is", answer)
} else {
fmt.Println("No answer was found")
}

The outer found get's never explicitly assigned to, yet it's legal and
sane code because of the default initialization promise. However, would the
compiler bark, the shadowing would become obvious.

This is how block scoping must work for it to be useful. Imagine for
example code getting moved around. There's no reason for the compiler to
say anything about it.

-j

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

People

Translate

site design / logo © 2021 Grokbase