This commit is contained in:
Mark McGranaghan 2012-09-20 20:44:54 -07:00
parent 04b705a58d
commit e79214dc5f
5 changed files with 35 additions and 18 deletions

View File

@ -6,7 +6,7 @@ func main() {
fmt.Println("Hello world") // It prints `Hello world`. fmt.Println("Hello world") // It prints `Hello world`.
} }
/* /* // Put this code in hello-world.go, then use
$ go run 01-hello.go // Use `go run` to run it. $ go run hello-world.go // `go run` to run it.
Hello world Hello world
*/ */

View File

@ -15,7 +15,7 @@ func main() {
} }
/* /*
$ go run 02-values.go $ go run values.go
Hello world Hello world
Hello other Hello other
1+1 = 2 1+1 = 2

View File

@ -3,8 +3,15 @@ package main
import "fmt" import "fmt"
func main() { func main() {
var x string = "Hello world" // `var` declares 1 or more variables. The ype comes var x string = "Hello world" // `var` declares 1 or more variables. The type comes
fmt.Println(x) // at the end. fmt.Println(x) // at the end.
var a, b int = 1, 2 // An example of declaring multiple ints variables. var a, b int = 1, 2 // An example of declaring multiple `int` variables.
fmt.Println(a, b)
} }
/*
$ go run variables.go
Hello world
1 2
*/

View File

@ -4,16 +4,25 @@ import "fmt"
func main() { func main() {
i := 1 // We initialize `i` with `1` and loop until it's 10. i := 1 // We initialize `i` with `1` and loop until it's 10.
for i <= 10 { for i <= 3 {
fmt.Println(i) fmt.Print(i)
i = i + 1 i = i + 1
} }
for j := 1; j <= 10; j++ { // That type of loop is common. We can do it on one for j := 1; j <= 3; j++ { // That type of loop is common. We can do it on one
fmt.Println(j) // line. fmt.Print(j) // line.
} }
for { // `for` without a condition will loop until you` for { // `for` without a condition will loop until you
return // return`. fmt.Println() // `return`.
return
} }
} // We'll see other `for` forms latter. } // We'll see other `for` forms latter.
/*
$ go run for.go
123123
*/
// == todo
// break out of for loop?

View File

@ -1,16 +1,17 @@
// If/Else
package main package main
import "fmt"
func main() { func main() {
if 7 % 2 == 0 { // If/else is straightford. Note that there are no fmt.Print("7 is ")
println("even") // enclosing parenthesis around the condition. if 7 % 2 == 0 { // If/else is straightford. Note that there are no
} else { // Also, there is no ternary operator (`?`) in Go. fmt.Println("even") // enclosing parenthesis around the condition.
println("odd") } else { // Also, there is no ternary operator (`?`) in Go.
fmt.Println("odd")
} }
} }
/* /*
$ go run 11-if-else.go $ go run 11-if-else.go
odd 7 is odd
*/ */