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

View File

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

View File

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

View File

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

View File

@ -1,2 +1,12 @@
$ go run for.go $ 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 // the compiler to e.g. check type-correctness of function
// calls. // calls.
@ -8,8 +8,7 @@ import "fmt"
func main() { func main() {
// `var` declares 1 or more variables. The type comes // `var` declares 1 or more variables.
// _after_ the name of the variable.
var a string = "Initial" var a string = "Initial"
fmt.Println(a) fmt.Println(a)
@ -22,7 +21,7 @@ func main() {
fmt.Println(d) fmt.Println(d)
// Variables declared without a corresponding // 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`. // zero value for an `int` is `0`.
var e int var e int
fmt.Println(e) fmt.Println(e)