From 2d56878c84262a279ca5ddded49979c0790acab7 Mon Sep 17 00:00:00 2001 From: Ryan Brainard Date: Sun, 20 Oct 2013 09:53:29 -0700 Subject: [PATCH 1/2] Move `state` variable internal to stateful goroutine. --- .../stateful-goroutines/stateful-goroutines.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/examples/stateful-goroutines/stateful-goroutines.go b/examples/stateful-goroutines/stateful-goroutines.go index 3af4d87..0b7d95c 100644 --- a/examples/stateful-goroutines/stateful-goroutines.go +++ b/examples/stateful-goroutines/stateful-goroutines.go @@ -36,12 +36,7 @@ type writeOp struct { func main() { - // The `state` will be a map as in the previous - // example. - var state = make(map[int]int) - - // Also as before we'll count how many operations we - // perform. + // As before we'll count how many operations we perform. var ops int64 = 0 // The `reads` and `writes` channels will be used by @@ -50,14 +45,18 @@ func main() { reads := make(chan *readOp) writes := make(chan *writeOp) - // Here is the goroutine that owns the `state`. This - // goroutine repeatedly selects on the `reads` and + // Here is the goroutine that owns the `state`, which + // is a map as in the previous example, but is held + // internally so all changes are handled by this function. + // This goroutine repeatedly selects on the `reads` and // `writes` channels, responding to requests as they // arrive. A response is executed by first performing // the requested operation and then sending a value // on the response channel `resp` to indicate success // (and the desired value in the case of `reads`). go func() { + var state = make(map[int]int) + for { select { case read := <-reads: From 846391c0a631180abbdde0e8a66cd8337ffe75d0 Mon Sep 17 00:00:00 2001 From: Mark McGranaghan Date: Sun, 20 Oct 2013 14:31:30 -0700 Subject: [PATCH 2/2] Tweak wording and remove trailing whitespace --- .../stateful-goroutines/stateful-goroutines.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/examples/stateful-goroutines/stateful-goroutines.go b/examples/stateful-goroutines/stateful-goroutines.go index 0b7d95c..24ba0e5 100644 --- a/examples/stateful-goroutines/stateful-goroutines.go +++ b/examples/stateful-goroutines/stateful-goroutines.go @@ -45,15 +45,15 @@ func main() { reads := make(chan *readOp) writes := make(chan *writeOp) - // Here is the goroutine that owns the `state`, which - // is a map as in the previous example, but is held - // internally so all changes are handled by this function. - // This goroutine repeatedly selects on the `reads` and - // `writes` channels, responding to requests as they - // arrive. A response is executed by first performing - // the requested operation and then sending a value - // on the response channel `resp` to indicate success - // (and the desired value in the case of `reads`). + // Here is the goroutine that owns the `state`, which + // is a map as in the previous example but now private + // to the stateful goroutine. This goroutine repeatedly + // selects on the `reads` and `writes` channels, + // responding to requests as they arrive. A response + // is executed by first performing the requested + // operation and then sending a value on the response + // channel `resp` to indicate success (and the desired + // value in the case of `reads`). go func() { var state = make(map[int]int)