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,8 +1,8 @@
Hello World
Values
Variables
# Constants
# For
Constants
For
If/Else
Switch
Arrays

View File

@ -1,13 +1,13 @@
// Go supports _constants_ of character, string, boolean,
// and numeric values.
package main
import "fmt"
// Use `const` to declare a constant value.
// Constants can be ...
const x string = "Hello World"
const s string = "Constant"
func main() {
fmt.Println(x)
fmt.Println(s)
}
// todo: research

View File

@ -1,2 +1,3 @@
$ go run constant.go
Hello World
Constant

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
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.

View File

@ -1,4 +1,4 @@
// In Go, variables are explicitly declared and used by
// In Go, _variables_ are explicitly declared and used by
// the compiler to e.g. check type-correctness of function
// calls.
@ -8,8 +8,7 @@ import "fmt"
func main() {
// `var` declares 1 or more variables. The type comes
// _after_ the name of the variable.
// `var` declares 1 or more variables.
var a string = "Initial"
fmt.Println(a)
@ -22,7 +21,7 @@ func main() {
fmt.Println(d)
// Variables declared without a corresponding
// initialization are _zero-valued_. For example the
// initialization are _zero-valued_. For example, the
// zero value for an `int` is `0`.
var e int
fmt.Println(e)