Back to questions
JuniorTheoryVery common
What does the program print?
package main
import "fmt"
func main() {
for i := range 3 {
fmt.Print(i)
}
}012
- Expecting
123:range 3counts from zero, soitakes the values 0, 1 and 2. - Expecting
0123or a compile error:for i := range nis a valid form offorsince Go 1.22, and it runs exactlyntimes.
- How does a labelled
breakdiffer from a labelledcontinue? - What scope does a variable declared in the
forinit clause have?