From 6a941f07bde0a8939058c1fc3610dc4cba782a06 Mon Sep 17 00:00:00 2001 From: Ihor Dutchak Date: Wed, 25 Nov 2020 03:15:02 +0200 Subject: [PATCH] Fix concurrency example compilation, plus extra example --- GoForCPPProgrammers.md | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/GoForCPPProgrammers.md b/GoForCPPProgrammers.md index a39e4c21..682da018 100644 --- a/GoForCPPProgrammers.md +++ b/GoForCPPProgrammers.md @@ -831,13 +831,21 @@ Here is an example of using a manager function to control access to a single value. ```go -type Cmd struct { Get bool; Val int } +type Cmd struct { + Get bool + Val int +} + func Manager(ch chan Cmd) { val := 0 for { c := <-ch - if c.Get { c.Val = val; ch <- c } - else { val = c.Val } + if c.Get { + c.Val = val + ch <- c + } else { + val = c.Val + } } } ``` @@ -850,13 +858,21 @@ instead. A solution is to pass in a channel. ```go -type Cmd2 struct { Get bool; Val int; Ch chan<- int } +type Cmd2 struct { + Get bool + Val int + Ch chan<- int +} + func Manager2(ch <-chan Cmd2) { val := 0 for { c := <-ch - if c.Get { c.Ch <- val } - else { val = c.Val } + if c.Get { + c.Ch <- val + } else { + val = c.Val + } } } ``` @@ -864,10 +880,18 @@ func Manager2(ch <-chan Cmd2) { To use ` Manager2 `, given a channel to it: ```go -func f4(ch chan<- Cmd2) int { +func getFromManagedChannel(ch chan<- Cmd2) int { myCh := make(chan int) c := Cmd2{true, 0, myCh} // Composite literal syntax. ch <- c return <-myCh } + +func main() { + ch := make(chan Cmd2) + go Manager2(ch) + // ... come code ... + currentValue := getFromManagedChannel(ch) + // ... some more code... +} ``` \ No newline at end of file