diff --git a/examples.txt b/examples.txt index 6af5458..2252437 100644 --- a/examples.txt +++ b/examples.txt @@ -1,8 +1,8 @@ Hello World Values Variables -# Constants -# For +Constants +For If/Else Switch Arrays diff --git a/examples/constants/constants.go b/examples/constants/constants.go index b5ae999..f35ae13 100644 --- a/examples/constants/constants.go +++ b/examples/constants/constants.go @@ -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 diff --git a/examples/constants/constants.sh b/examples/constants/constants.sh index f7a4866..8d262c8 100644 --- a/examples/constants/constants.sh +++ b/examples/constants/constants.sh @@ -1,2 +1,3 @@ $ go run constant.go -Hello World +Constant + diff --git a/examples/for/for.go b/examples/for/for.go index 8248240..7bdbabf 100644 --- a/examples/for/for.go +++ b/examples/for/for.go @@ -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? diff --git a/examples/for/for.sh b/examples/for/for.sh index cf48b16..b6ebe44 100644 --- a/examples/for/for.sh +++ b/examples/for/for.sh @@ -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. diff --git a/examples/variables/variables.go b/examples/variables/variables.go index 0398429..9f3700b 100644 --- a/examples/variables/variables.go +++ b/examples/variables/variables.go @@ -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)