adding simpler channels koans as prelude to concurrency

This commit is contained in:
Steven Degutis
2012-03-26 17:31:15 -05:00
parent 2e07ddc006
commit dedbcb2fcc
2 changed files with 28 additions and 0 deletions

27
about_channels.go Normal file
View File

@@ -0,0 +1,27 @@
package go_koans
func aboutChannels() {
ch := make(chan string, 2)
assert(len(ch) == __int__) // channels are like buffers
ch<- "foo" // i mean, "metaphors are like similes"
assert(len(ch) == __int__) // they can be queried for queued items
assert(<-ch == __string__) // items can be popped out of them
assert(len(ch) == __int__) // and len() always reflects the "current" queue status
// the 'go' keyword runs a function-call in a new "goroutine"
// which executes "concurrently" with the calling "goroutine"
go func() {
// your code goes here
}()
assert(__delete_me__) // we'll need to make room for the queue, or suffer deadlocks
ch<- "bar" // this send will succeed
ch<- "quux" // there's enough room for this send too
ch<- "extra" // but the buffer only has two slots
}

View File

@@ -33,6 +33,7 @@ func TestKoans(t *testing.T) {
aboutPointers()
aboutStructs()
aboutAllocation()
aboutChannels()
aboutConcurrency()
aboutPanics()