This commit is contained in:
Mark McGranaghan 2012-09-23 12:53:50 -07:00
parent 6750b72724
commit 618dec4fae
13 changed files with 58 additions and 23 deletions

View File

@ -1,3 +1,5 @@
// ## New
package main
import "fmt"

View File

@ -1,3 +1,5 @@
// ## Structs
package main
import "fmt"

View File

@ -1,3 +1,5 @@
// ## Struct Fields
package main
import "fmt"

View File

@ -1,3 +1,5 @@
// ## Methods
package main
import "fmt"

View File

@ -1,7 +1,9 @@
// ## Embedding
package main
import "fmt"
import "math"
import "fmt"
type Shape interface {
area() float64

View File

@ -1,7 +1,9 @@
// ## Interfaces
package main
import "fmt"
type Shape interface {
area() float64
}
}

View File

@ -1,3 +1,5 @@
// ## Goroutines
package main
import "fmt"

View File

@ -1,10 +1,10 @@
// ## Concurrent Goroutines
package main
import (
"fmt"
"time"
"math/rand"
)
import "time"
import "math/rand"
import "fmt"
func f(n int) {
for i := 0; i < 10; i++ {

View File

@ -1,3 +1,5 @@
// ## Channels
package main
import "fmt"

View File

@ -1,3 +1,5 @@
// ## Channel Buffering
package main
import "fmt"

View File

@ -1,3 +1,5 @@
// ## Channel Directions
package main
import "fmt"

View File

@ -1,23 +1,32 @@
package main // We can use channels to synchronize execution
// accross goroutines. Here's an example of waiting
import "fmt" // for another goroutine to finish.
// ## Synchronization
// We can use channels to synchronize execution
// accross goroutines. Here's an example of waiting
// for another goroutine to finish.
package main
import "fmt"
import "time"
func worker(done chan<- bool) { // The `done` channel will be used for
fmt.Print("Working...") // synchronization.
// The `done` channel will be used for
// synchronization.
func worker(done chan<- bool) {
fmt.Print("Working...")
time.Sleep(time.Second)
fmt.Println(" done")
done <- true // Send a value to notify that the work is done.
// Send a value to notify that the work is done.
done <- true
}
func main() {
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
} // over the channel.
/*
$ go run synchronization.go // If you commented out the `<- done` line, the
Working... done // program would exit before the `worker` even
*/ // started.
// Start a worker goroutine, give it the channel to
// notify on.
done := make(chan bool, 1)
go worker(done)
// Block until we can receive a value from the worker
// over the channel.
<- done
}

View File

@ -0,0 +1,6 @@
$ go run synchronization.go
Working... done
# If you commented out the `<- done` line, the
# program would exit before the `worker` even
# started.