golang if initialization statement scoped to inner if block. Why? : <p>I've found a bug in my code</p>

<pre><code>func receive() (err error) {
    if v, err := produce(); err == nil {
        fmt.Println("value: ", v)
    }
    return
}
</code></pre>

<p>Error is never returning from this function, but I was absolutely sure it should.</p>

<p>After some testing I understood that <code>err</code> is redeclared in the if statement. More than that - all variables are always redeclared in short variable assignment inside <code>if</code> statement, despite they were declared before.</p>

<p>This is working code</p>

<pre><code>func receive() (err error) {
    v, err := produce()
    if err == nil {
        fmt.Println("value: ", v)
    }
    return
}
</code></pre>

<p>Here is an example: <a href="https://play.golang.org/p/1AWBsPbLiI1" rel="nofollow noreferrer">https://play.golang.org/p/1AWBsPbLiI1</a></p>

<p>Seems like if statement </p>

<pre><code>//some code
if &lt;init_statement&gt;; &lt;expression&gt; {}
//more code
</code></pre>

<p>is equivalent to </p>

<pre><code>//some code
{
    &lt;init_statement&gt;
    if expression {}
}
//more code
</code></pre>

<p>So, my questions:</p>

<p>1) why existing variables are not used</p>

<p>2) why such scoping is not mentioned in documentation/language spec</p>

<p>3) why compiler doesn't say that no one returns a value</p>
