formatting
This commit is contained in:
parent
5382f4fa9c
commit
5b7c5e7f3f
30
008-for.go
30
008-for.go
@ -1,23 +1,23 @@
|
||||
package main // `for` is Go's only looping construct. Below are
|
||||
// two common forms.
|
||||
package main // `for` is Go's only looping construct. Below are
|
||||
// two common forms.
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
i := 1 // We initialize `i` with `1` and loop until it's 10.
|
||||
for i <= 3 {
|
||||
fmt.Print(i)
|
||||
i = i + 1
|
||||
}
|
||||
i := 1 // Initialize `i` with `1` and loop until it's 10.
|
||||
for i <= 3 {
|
||||
fmt.Print(i)
|
||||
i = i + 1
|
||||
}
|
||||
|
||||
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
|
||||
fmt.Println() // `return`.
|
||||
return
|
||||
for j := 1; j <= 3; j++ { // That type of loop is common. We can do it on one
|
||||
fmt.Print(j) // line.
|
||||
}
|
||||
} // We'll see other `for` forms latter.
|
||||
|
||||
for { // `for` without a condition will loop until you
|
||||
fmt.Println() // `return`.
|
||||
return
|
||||
}
|
||||
} // We'll see other `for` forms latter.
|
||||
|
||||
/*
|
||||
$ go run for.go
|
||||
|
@ -4,8 +4,8 @@ import "fmt"
|
||||
|
||||
func main() {
|
||||
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.
|
||||
if 7 % 2 == 0 { // If/else is straight-forward. Note that there are no
|
||||
fmt.Println("even") // enclosing parentheses around the condition.
|
||||
} else { // Also, there is no ternary operator (`?`) in Go.
|
||||
fmt.Println("odd")
|
||||
}
|
||||
|
@ -1,20 +1,20 @@
|
||||
package main // We can use channels to synchronize execution
|
||||
// accross goroutings. Here's an example of waiting
|
||||
import "fmt" // for another gorouting to finish.
|
||||
// accross goroutines. Here's an example of waiting
|
||||
import "fmt" // for another goroutine to finish.
|
||||
import "time"
|
||||
|
||||
func worker(done chan<- bool) { // The `done` channel will be used for
|
||||
fmt.Print("Working...") // synchronization.
|
||||
time.Sleep(time.Second)
|
||||
fmt.Println(" done")
|
||||
done <- true // Send a value to notify that the work is done.
|
||||
done <- true // Send a value to notify that the work is done.
|
||||
}
|
||||
|
||||
func main() {
|
||||
done := make(chan bool, 1) // Start a worker goroutine, give it the channel to
|
||||
done := make(chan bool, 1) // Start a worker goroutine, give it the channel to
|
||||
go worker(done) // notify on.
|
||||
|
||||
<- done // Block until we can receive a value from the worker
|
||||
<- done // Block until we can receive a value from the worker
|
||||
} // over the channel.
|
||||
|
||||
/*
|
||||
|
@ -16,7 +16,7 @@ func main() {
|
||||
out := os.Stdout
|
||||
|
||||
for { // If succesful, each `ReadLine` returns bytes and a
|
||||
inBytes, pfx, err := in.ReadLine() // boolean indicating if don't have the whole line.
|
||||
inBytes, pfx, err := in.ReadLine() // boolean indicating if don't have the whole line yet.
|
||||
|
||||
if err == io.EOF { // The `EOF` error is expected when we reach the end
|
||||
return // of the input, so exit gracefully in that case.
|
||||
|
Loading…
x
Reference in New Issue
Block a user