From 6d8aa9b50a20c4caa93b7e41f86aeb6961351c59 Mon Sep 17 00:00:00 2001 From: Eli Bendersky Date: Tue, 4 Jun 2019 15:56:59 -0700 Subject: [PATCH 1/2] Remove pointers from the stateful-goroutines example. The pointers are unnecessary and make the sample slightly more complicated. Fixes #221 --- .../stateful-goroutines.go | 9 ++++---- .../stateful-goroutines.hash | 4 ++-- public/stateful-goroutines | 23 ++++++++++++++----- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/examples/stateful-goroutines/stateful-goroutines.go b/examples/stateful-goroutines/stateful-goroutines.go index 598c4a9..59655bb 100644 --- a/examples/stateful-goroutines/stateful-goroutines.go +++ b/examples/stateful-goroutines/stateful-goroutines.go @@ -28,6 +28,7 @@ type readOp struct { key int resp chan int } + type writeOp struct { key int val int @@ -43,8 +44,8 @@ func main() { // The `reads` and `writes` channels will be used by // other goroutines to issue read and write requests, // respectively. - reads := make(chan *readOp) - writes := make(chan *writeOp) + 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 now private @@ -76,7 +77,7 @@ func main() { for r := 0; r < 100; r++ { go func() { for { - read := &readOp{ + read := readOp{ key: rand.Intn(5), resp: make(chan int)} reads <- read @@ -92,7 +93,7 @@ func main() { for w := 0; w < 10; w++ { go func() { for { - write := &writeOp{ + write := writeOp{ key: rand.Intn(5), val: rand.Intn(100), resp: make(chan bool)} diff --git a/examples/stateful-goroutines/stateful-goroutines.hash b/examples/stateful-goroutines/stateful-goroutines.hash index f88cfa8..4956f93 100644 --- a/examples/stateful-goroutines/stateful-goroutines.hash +++ b/examples/stateful-goroutines/stateful-goroutines.hash @@ -1,2 +1,2 @@ -ff8b7e93a43c284ada17f4387cc5dfeafa3ee316 -dlfBGF-qff6 +330fde708b006d5571f9b94e7a63b4fead62bd36 +DxITfgl__z9 diff --git a/public/stateful-goroutines b/public/stateful-goroutines index 17add47..144f1e9 100644 --- a/public/stateful-goroutines +++ b/public/stateful-goroutines @@ -34,7 +34,7 @@ by exactly 1 goroutine.

- +
package main
 
@@ -76,7 +76,18 @@ goroutine to respond.

key int resp chan int } -type writeOp struct { + + + + + + + + + + + +
type writeOp struct {
     key  int
     val  int
     resp chan bool
@@ -121,8 +132,8 @@ respectively.

-
    reads := make(chan *readOp)
-    writes := make(chan *writeOp)
+            
    reads := make(chan readOp)
+    writes := make(chan writeOp)
 
@@ -174,7 +185,7 @@ result over the provided resp channel.

    for r := 0; r < 100; r++ {
         go func() {
             for {
-                read := &readOp{
+                read := readOp{
                     key:  rand.Intn(5),
                     resp: make(chan int)}
                 reads <- read
@@ -200,7 +211,7 @@ approach.

    for w := 0; w < 10; w++ {
         go func() {
             for {
-                write := &writeOp{
+                write := writeOp{
                     key:  rand.Intn(5),
                     val:  rand.Intn(100),
                     resp: make(chan bool)}

From f99adfc228194d44bab6f736a1a118839cff7c10 Mon Sep 17 00:00:00 2001
From: Eli Bendersky 
Date: Tue, 4 Jun 2019 16:27:55 -0700
Subject: [PATCH 2/2] Keep the two structs together for presentation

---
 .../stateful-goroutines/stateful-goroutines.go    |  1 -
 .../stateful-goroutines/stateful-goroutines.hash  |  4 ++--
 public/stateful-goroutines                        | 15 ++-------------
 3 files changed, 4 insertions(+), 16 deletions(-)

diff --git a/examples/stateful-goroutines/stateful-goroutines.go b/examples/stateful-goroutines/stateful-goroutines.go
index 59655bb..cb8b347 100644
--- a/examples/stateful-goroutines/stateful-goroutines.go
+++ b/examples/stateful-goroutines/stateful-goroutines.go
@@ -28,7 +28,6 @@ type readOp struct {
 	key  int
 	resp chan int
 }
-
 type writeOp struct {
 	key  int
 	val  int
diff --git a/examples/stateful-goroutines/stateful-goroutines.hash b/examples/stateful-goroutines/stateful-goroutines.hash
index 4956f93..1f3eb48 100644
--- a/examples/stateful-goroutines/stateful-goroutines.hash
+++ b/examples/stateful-goroutines/stateful-goroutines.hash
@@ -1,2 +1,2 @@
-330fde708b006d5571f9b94e7a63b4fead62bd36
-DxITfgl__z9
+956afe7524b492b2e85f8320c70f180c448a764a
+saQTLpdIgp2
diff --git a/public/stateful-goroutines b/public/stateful-goroutines
index 144f1e9..f684680 100644
--- a/public/stateful-goroutines
+++ b/public/stateful-goroutines
@@ -34,7 +34,7 @@ by exactly 1 goroutine.

- +
package main
 
@@ -76,18 +76,7 @@ goroutine to respond.

key int resp chan int } -
- - - - - - - - - - -
type writeOp struct {
+type writeOp struct {
     key  int
     val  int
     resp chan bool