publish constants and for

This commit is contained in:
Mark McGranaghan
2012-10-17 13:25:27 -07:00
parent 777c529caa
commit 1632105ca8
6 changed files with 36 additions and 29 deletions

View File

@@ -1,32 +1,29 @@
// `for` is Go's only looping construct. Here are
// two common forms.
// three basic types of `for` loops.
package main
import "fmt"
func main() {
// Initialize `i` with `1` and loop until it's 10.
// The most basic type, with a single condition.
i := 1
for i <= 3 {
fmt.Print(i)
fmt.Println(i)
i = i + 1
}
// That type of loop is common. We can do it on one
// line.
for j := 1; j <= 3; j++ {
fmt.Print(j)
// A classic initial/condition/after `for` loop.
for j := 7; j <= 9; j++ {
fmt.Println(j)
}
// `for` without a condition will loop until you
// `return`.
// `for` without a condition will loop repeatedly
// until you `break` out of the loop or `return` from
// the enclosing function.
for {
fmt.Println()
return
fmt.Println("loop")
break
}
}
// We'll see other `for` forms latter.
// todo: break out of for loop?

View File

@@ -1,2 +1,12 @@
$ go run for.go
123123
$ go run for.go
1
2
3
7
8
9
loop
# We'll see some other `for` forms latter when we look at
# `range` statements, channels, and other data
# structures.