Skip to content

Commit e753aa3

Browse files
committed
Update Go-Concurrency-Patterns
1 parent b6698ef commit e753aa3

File tree

9 files changed

+249
-0
lines changed

9 files changed

+249
-0
lines changed

Go-Concurrency-Patterns/README.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
### Here is the **video**
2+
3+
[Go Concurrency Patterns](https://www.youtube.com/watch?v=f6kdp27TYZs)
4+
5+
### Here is some pic
6+
7+
#### [boring-04.go](boring-04/boring-04.go)
8+
9+
![2>1](images/chan.jpeg)
10+
11+
```go
12+
func fanIn(input1, input2 <-chan string) <-chan string {
13+
c := make(chan string)
14+
go func() {
15+
for {
16+
c <- <-input1
17+
}
18+
}()
19+
go func() {
20+
for {
21+
c <- <-input2
22+
}
23+
}()
24+
return c
25+
}
26+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"math/rand"
6+
"time"
7+
)
8+
9+
func main() {
10+
boring("boring!")
11+
}
12+
13+
func boring(msg string) {
14+
for i := 0; ; i++ {
15+
fmt.Println(msg, i)
16+
time.Sleep(time.Duration(rand.Intn(1e3)) * time.Millisecond)
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"math/rand"
6+
"time"
7+
)
8+
9+
func main() {
10+
c := make(chan string)
11+
go boring("boring!", c)
12+
for i := 0; i < 5; i++ {
13+
fmt.Printf("you say: %q\n", <-c)
14+
}
15+
fmt.Println("You're boring; I'm leaving.")
16+
}
17+
18+
func boring(msg string, c chan string) {
19+
for i := 0; ; i++ {
20+
c <- fmt.Sprintf("%s %d", msg, i)
21+
time.Sleep(time.Duration(rand.Intn(1e3)) * time.Millisecond)
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"math/rand"
6+
"time"
7+
)
8+
9+
func main() {
10+
c := boring("boring!")
11+
d := boring("boring!")
12+
for i := 0; i < 5; i++ {
13+
fmt.Printf("you say: %q\n", <-c)
14+
fmt.Printf("you say: %q\n", <-d)
15+
}
16+
fmt.Println("You're boring; I'm leaving.")
17+
}
18+
19+
func boring(msg string) <-chan string {
20+
c := make(chan string)
21+
go func() {
22+
for i := 0; ; i++ {
23+
c <- fmt.Sprintf("%s %d", msg, i)
24+
time.Sleep(time.Duration(rand.Intn(1e3)) * time.Millisecond)
25+
}
26+
}()
27+
return c
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"math/rand"
6+
"time"
7+
)
8+
9+
func main() {
10+
c := fanIn(boring("Joe"), boring("Ann"))
11+
for i := 0; i < 10; i++ {
12+
fmt.Println(<-c)
13+
}
14+
fmt.Println("You're both boring; I'm leaving.")
15+
}
16+
17+
func fanIn(input1, input2 <-chan string) <-chan string {
18+
c := make(chan string)
19+
go func() {
20+
for {
21+
c <- <-input1
22+
}
23+
}()
24+
go func() {
25+
for {
26+
c <- <-input2
27+
}
28+
}()
29+
return c
30+
}
31+
32+
func boring(msg string) <-chan string {
33+
c := make(chan string)
34+
go func() {
35+
for i := 0; ; i++ {
36+
c <- fmt.Sprintf("%s %d", msg, i)
37+
time.Sleep(time.Duration(rand.Intn(2e3)) * time.Millisecond)
38+
}
39+
}()
40+
return c
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"math/rand"
6+
"time"
7+
)
8+
9+
type Message struct {
10+
str string
11+
wait chan bool
12+
}
13+
14+
func main() {
15+
c := fanIn(boring("Joe"), boring("Ann"))
16+
for i := 0; i < 5; i++ {
17+
msg1 := <-c
18+
fmt.Println(msg1.str)
19+
msg2 := <-c
20+
fmt.Println(msg2.str)
21+
22+
msg1.wait <- true
23+
msg2.wait <- true
24+
}
25+
fmt.Println("You're both boring; I'm leaving.")
26+
}
27+
28+
func fanIn(input1, input2 <-chan Message) <-chan Message {
29+
c := make(chan Message)
30+
go func() {
31+
for {
32+
c <- <-input1
33+
}
34+
}()
35+
go func() {
36+
for {
37+
c <- <-input2
38+
}
39+
}()
40+
return c
41+
}
42+
43+
func boring(msg string) <-chan Message {
44+
c := make(chan Message)
45+
waitForIt := make(chan bool)
46+
go func() {
47+
for i := 0; ; i++ {
48+
c <- Message{fmt.Sprintf("%s: %d", msg, i), waitForIt}
49+
time.Sleep(time.Duration(rand.Intn(2e3)) * time.Millisecond)
50+
<-waitForIt
51+
}
52+
}()
53+
return c
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
var c1, c2, c3 chan int
7+
8+
select {
9+
case v1 := <-c1:
10+
fmt.Printf("received %v from c1\n", v1)
11+
case v2 := <-c2:
12+
fmt.Printf("received %v from c2\n", v2)
13+
case c3 <- 23:
14+
fmt.Printf("sent %v to c3\n", 23)
15+
default:
16+
fmt.Printf("no one was ready to communicate\n")
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"math/rand"
6+
"time"
7+
)
8+
9+
func main() {
10+
c := fanIn(boring("Joe"), boring("Ann"))
11+
for i := 0; i < 10; i++ {
12+
fmt.Println(<-c)
13+
}
14+
fmt.Println("You're both boring; I'm leaving.")
15+
}
16+
17+
func boring(msg string) <-chan string { // Returns receive-only channel of strings.
18+
c := make(chan string)
19+
go func() { // We launch the goroutine from inside the function.
20+
for i := 0; ; i++ {
21+
c <- fmt.Sprintf("%s: %d", msg, i)
22+
time.Sleep(time.Duration(rand.Intn(2e3)) * time.Millisecond)
23+
}
24+
}()
25+
return c // Return the channel to the caller.
26+
}
27+
28+
func fanIn(input1, input2 <-chan string) <-chan string {
29+
c := make(chan string)
30+
go func() {
31+
for {
32+
select {
33+
case s := <-input1:
34+
c <- s
35+
case s := <-input2:
36+
c <- s
37+
}
38+
}
39+
}()
40+
return c
41+
}
65.1 KB
Loading

0 commit comments

Comments
 (0)