From c67921cb0edcc9761a800a8078886899aed6871c Mon Sep 17 00:00:00 2001
From: Niall Grant
Date: Tue, 5 May 2020 17:09:39 +0100
Subject: [PATCH] Improve Channel Direction example
---
.../channel-directions/channel-directions.go | 28 ++++++++-------
.../channel-directions.hash | 4 +--
.../channel-directions/channel-directions.sh | 4 ++-
public/channel-directions | 36 ++++++++++---------
4 files changed, 40 insertions(+), 32 deletions(-)
diff --git a/examples/channel-directions/channel-directions.go b/examples/channel-directions/channel-directions.go
index 5f2786c..0636baa 100644
--- a/examples/channel-directions/channel-directions.go
+++ b/examples/channel-directions/channel-directions.go
@@ -7,24 +7,26 @@ package main
import "fmt"
-// This `ping` function only accepts a channel for sending
+// This `send` function only accepts a channel for sending
// values. It would be a compile-time error to try to
// receive on this channel.
-func ping(pings chan<- string, msg string) {
- pings <- msg
+func send(sendCh chan<- string, msg string) {
+ fmt.Println("send message:", msg)
+ sendCh <- msg
}
-// The `pong` function accepts one channel for receives
-// (`pings`) and a second for sends (`pongs`).
-func pong(pings <-chan string, pongs chan<- string) {
- msg := <-pings
- pongs <- msg
+// The `fwd` function accepts one channel for receives
+// (`sendCh`) and a second for sends (`forwardCh`).
+func fwd(sendCh <-chan string, forwardCh chan<- string) {
+ msg := <-sendCh
+ fmt.Println("forwarding:", msg)
+ forwardCh <- msg
}
func main() {
- pings := make(chan string, 1)
- pongs := make(chan string, 1)
- ping(pings, "passed message")
- pong(pings, pongs)
- fmt.Println(<-pongs)
+ sendCh := make(chan string, 1)
+ forwardCh := make(chan string, 1)
+ send(sendCh, "Hello, World!")
+ fwd(sendCh, forwardCh)
+ fmt.Println("reading messages: ", <-forwardCh)
}
diff --git a/examples/channel-directions/channel-directions.hash b/examples/channel-directions/channel-directions.hash
index b848781..53b2309 100644
--- a/examples/channel-directions/channel-directions.hash
+++ b/examples/channel-directions/channel-directions.hash
@@ -1,2 +1,2 @@
-d1b1580f72c3c101ea46480e6c2361f4f96b049a
-mjNJDHwUH4R
+3c2151473e3a75fb2318566ad1d5528919cd47fa
+tyxhbRAiXMa
diff --git a/examples/channel-directions/channel-directions.sh b/examples/channel-directions/channel-directions.sh
index 8db5cb6..4d42227 100644
--- a/examples/channel-directions/channel-directions.sh
+++ b/examples/channel-directions/channel-directions.sh
@@ -1,2 +1,4 @@
$ go run channel-directions.go
-passed message
+send message: Hello, World!
+forwarding: Hello, World!
+reading messages: Hello, World!
diff --git a/public/channel-directions b/public/channel-directions
index 2814e81..2c8b046 100644
--- a/public/channel-directions
+++ b/public/channel-directions
@@ -44,7 +44,7 @@ the program.
- 
+ 
@@ -65,15 +65,16 @@ the program.
|
- This ping function only accepts a channel for sending
+ This send function only accepts a channel for sending
values. It would be a compile-time error to try to
receive on this channel.
|
- func ping(pings chan<- string, msg string) {
- pings <- msg
+ func send(sendCh chan<- string, msg string) {
+ fmt.Println("send message:", msg)
+ sendCh <- msg
}
@@ -82,15 +83,16 @@ receive on this channel.
- The pong function accepts one channel for receives
-(pings ) and a second for sends (pongs ).
+ The fwd function accepts one channel for receives
+(sendCh ) and a second for sends (forwardCh ).
|
- func pong(pings <-chan string, pongs chan<- string) {
- msg := <-pings
- pongs <- msg
+ func fwd(sendCh <-chan string, forwardCh chan<- string) {
+ msg := <-sendCh
+ fmt.Println("forwarding:", msg)
+ forwardCh <- msg
}
@@ -104,11 +106,11 @@ receive on this channel.
func main() {
- pings := make(chan string, 1)
- pongs := make(chan string, 1)
- ping(pings, "passed message")
- pong(pings, pongs)
- fmt.Println(<-pongs)
+ sendCh := make(chan string, 1)
+ forwardCh := make(chan string, 1)
+ send(sendCh, "Hello, World!")
+ fwd(sendCh, forwardCh)
+ fmt.Println("reading messages: ", <-forwardCh)
}
@@ -126,7 +128,9 @@ receive on this channel.
|
$ go run channel-directions.go
-passed message
+send message: Hello, World!
+forwarding: Hello, World!
+reading messages: Hello, World!
|
@@ -145,7 +149,7 @@ receive on this channel.
| |