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`.
}
/*
$ go run 01-hello.go // Use `go run` to run it.
/* // Put this code in hello-world.go, then use
$ go run hello-world.go // `go run` to run it.
Hello world
*/

View File

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

View File

@ -3,8 +3,15 @@ package main
import "fmt"
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.
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() {
i := 1 // We initialize `i` with `1` and loop until it's 10.
for i <= 10 {
fmt.Println(i)
for i <= 3 {
fmt.Print(i)
i = i + 1
}
for j := 1; j <= 10; j++ { // That type of loop is common. We can do it on one
fmt.Println(j) // line.
for j := 1; j <= 3; j++ { // That type of loop is common. We can do it on one
fmt.Print(j) // line.
}
for { // `for` without a condition will loop until you`
return // return`.
for { // `for` without a condition will loop until you
fmt.Println() // `return`.
return
}
} // 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
import "fmt"
func main() {
if 7 % 2 == 0 { // If/else is straightford. Note that there are no
println("even") // enclosing parenthesis around the condition.
} else { // Also, there is no ternary operator (`?`) in Go.
println("odd")
fmt.Print("7 is ")
if 7 % 2 == 0 { // If/else is straightford. Note that there are no
fmt.Println("even") // enclosing parenthesis around the condition.
} else { // Also, there is no ternary operator (`?`) in Go.
fmt.Println("odd")
}
}
/*
$ go run 11-if-else.go
odd
7 is odd
*/