Back to questions
JuniorTheoryCommon
What happens when this program is built and run?
package main
import "fmt"
func main() {
if v := 10; v > 5 {
v++
}
fmt.Println(v)
}It does not compile
- Expecting
11: a variable declared in theifinit statement exists only inside theifand itselse; after the closing bracevis undefined. - Expecting a zero-valued
vafter theif: there is no outervat all, so the compiler stops withundefined: v.
- How would you change the program so that it prints
11? - Which other statements in Go let you declare a variable for that statement only?