From 618dec4fae8a4536405316a5c28579935041d205 Mon Sep 17 00:00:00 2001 From: Mark McGranaghan Date: Sun, 23 Sep 2012 12:53:50 -0700 Subject: [PATCH] updates --- 028-new/{028-new.go => new.go} | 2 + 029-structs/{029-structs.go => structs.go} | 2 + 030-fields/{030-fields.go => fields.go} | 2 + 031-methods/{031-methods.go => methods.go} | 2 + .../{032-embedding.go => embedding.go} | 4 +- .../{033-interfaces.go => interfaces.go} | 4 +- .../{034-goroutines.go => goroutines.go} | 2 + .../{035-concurrency.go => concurrency.go} | 10 ++--- 036-channels/{036-channels.go => channels.go} | 2 + .../{037-buffering.go => buffering.go} | 2 + .../{038-directions.go => directions.go} | 2 + 039-synchronization/synchronization.go | 41 +++++++++++-------- 039-synchronization/synchronization.sh | 6 +++ 13 files changed, 58 insertions(+), 23 deletions(-) rename 028-new/{028-new.go => new.go} (94%) rename 029-structs/{029-structs.go => structs.go} (95%) rename 030-fields/{030-fields.go => fields.go} (89%) rename 031-methods/{031-methods.go => methods.go} (97%) rename 032-embedding/{032-embedding.go => embedding.go} (98%) rename 033-interfaces/{033-interfaces.go => interfaces.go} (77%) rename 034-goroutines/{034-goroutines.go => goroutines.go} (91%) rename 035-concurrency/{035-concurrency.go => concurrency.go} (80%) rename 036-channels/{036-channels.go => channels.go} (91%) rename 037-buffering/{037-buffering.go => buffering.go} (86%) rename 038-directions/{038-directions.go => directions.go} (95%) create mode 100644 039-synchronization/synchronization.sh diff --git a/028-new/028-new.go b/028-new/new.go similarity index 94% rename from 028-new/028-new.go rename to 028-new/new.go index ad2a807..4c98b62 100644 --- a/028-new/028-new.go +++ b/028-new/new.go @@ -1,3 +1,5 @@ +// ## New + package main import "fmt" diff --git a/029-structs/029-structs.go b/029-structs/structs.go similarity index 95% rename from 029-structs/029-structs.go rename to 029-structs/structs.go index 624d904..5b04ffd 100644 --- a/029-structs/029-structs.go +++ b/029-structs/structs.go @@ -1,3 +1,5 @@ +// ## Structs + package main import "fmt" diff --git a/030-fields/030-fields.go b/030-fields/fields.go similarity index 89% rename from 030-fields/030-fields.go rename to 030-fields/fields.go index 6876fab..6a59480 100644 --- a/030-fields/030-fields.go +++ b/030-fields/fields.go @@ -1,3 +1,5 @@ +// ## Struct Fields + package main import "fmt" diff --git a/031-methods/031-methods.go b/031-methods/methods.go similarity index 97% rename from 031-methods/031-methods.go rename to 031-methods/methods.go index 2579995..196b8a4 100644 --- a/031-methods/031-methods.go +++ b/031-methods/methods.go @@ -1,3 +1,5 @@ +// ## Methods + package main import "fmt" diff --git a/032-embedding/032-embedding.go b/032-embedding/embedding.go similarity index 98% rename from 032-embedding/032-embedding.go rename to 032-embedding/embedding.go index 908c6ce..c5a1f96 100644 --- a/032-embedding/032-embedding.go +++ b/032-embedding/embedding.go @@ -1,7 +1,9 @@ +// ## Embedding + package main -import "fmt" import "math" +import "fmt" type Shape interface { area() float64 diff --git a/033-interfaces/033-interfaces.go b/033-interfaces/interfaces.go similarity index 77% rename from 033-interfaces/033-interfaces.go rename to 033-interfaces/interfaces.go index 0ebcbf2..590b986 100644 --- a/033-interfaces/033-interfaces.go +++ b/033-interfaces/interfaces.go @@ -1,7 +1,9 @@ +// ## Interfaces + package main import "fmt" type Shape interface { area() float64 -} \ No newline at end of file +} diff --git a/034-goroutines/034-goroutines.go b/034-goroutines/goroutines.go similarity index 91% rename from 034-goroutines/034-goroutines.go rename to 034-goroutines/goroutines.go index e0bc17e..e23a905 100644 --- a/034-goroutines/034-goroutines.go +++ b/034-goroutines/goroutines.go @@ -1,3 +1,5 @@ +// ## Goroutines + package main import "fmt" diff --git a/035-concurrency/035-concurrency.go b/035-concurrency/concurrency.go similarity index 80% rename from 035-concurrency/035-concurrency.go rename to 035-concurrency/concurrency.go index 6090f05..64235fa 100644 --- a/035-concurrency/035-concurrency.go +++ b/035-concurrency/concurrency.go @@ -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++ { diff --git a/036-channels/036-channels.go b/036-channels/channels.go similarity index 91% rename from 036-channels/036-channels.go rename to 036-channels/channels.go index 102c089..4c2f135 100644 --- a/036-channels/036-channels.go +++ b/036-channels/channels.go @@ -1,3 +1,5 @@ +// ## Channels + package main import "fmt" diff --git a/037-buffering/037-buffering.go b/037-buffering/buffering.go similarity index 86% rename from 037-buffering/037-buffering.go rename to 037-buffering/buffering.go index a9c29e5..9126111 100644 --- a/037-buffering/037-buffering.go +++ b/037-buffering/buffering.go @@ -1,3 +1,5 @@ +// ## Channel Buffering + package main import "fmt" diff --git a/038-directions/038-directions.go b/038-directions/directions.go similarity index 95% rename from 038-directions/038-directions.go rename to 038-directions/directions.go index 14a399c..91e3096 100644 --- a/038-directions/038-directions.go +++ b/038-directions/directions.go @@ -1,3 +1,5 @@ +// ## Channel Directions + package main import "fmt" diff --git a/039-synchronization/synchronization.go b/039-synchronization/synchronization.go index 690fad0..de17241 100644 --- a/039-synchronization/synchronization.go +++ b/039-synchronization/synchronization.go @@ -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 +} diff --git a/039-synchronization/synchronization.sh b/039-synchronization/synchronization.sh new file mode 100644 index 0000000..9a69de0 --- /dev/null +++ b/039-synchronization/synchronization.sh @@ -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.