Skip to content

Commit 56078ad

Browse files
committed
inital commit
0 parents  commit 56078ad

14 files changed

+248
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.DS_Store
2+
.idea

fan_in.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package main
2+
3+
func BoringFanIn(chans ... <-chan string) <-chan string {
4+
c := make(chan string)
5+
for _, v := range chans {
6+
go func(<-chan string) { c <- <-v }(v)
7+
}
8+
return c
9+
}

fan_in_fixed_arguments.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package main
2+
3+
func BoringFanInFixedArguments(input1, input2 <-chan string) <-chan string {
4+
c := make(chan string)
5+
6+
go func() {
7+
for {
8+
select {
9+
case s := <-input1: c <- s
10+
case s := <-input2: c <- s
11+
}
12+
}
13+
}()
14+
15+
return c
16+
}

fan_out_all.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package main
2+
3+
func FanOutAll(input <-chan string, outs... chan<- string) {
4+
go func() {
5+
for {
6+
msg := <-input
7+
for _, out := range outs {
8+
go func(o chan<-string) {
9+
o <- msg
10+
}(out)
11+
}
12+
}
13+
}()
14+
}

fan_out_any.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package main
2+
3+
func FanOutAny(input <-chan string, out1, out2 chan<- string) {
4+
go func() {
5+
for {
6+
msg := <-input
7+
select {
8+
case out1 <- msg:
9+
case out2 <- msg:
10+
}
11+
}
12+
}()
13+
}

generator.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"time"
6+
)
7+
8+
func BoringGenerator(msg string) <-chan string {
9+
c := make(chan string)
10+
11+
go func(){
12+
for i := 0; ; i++ {
13+
c <- fmt.Sprintf("%s %d", msg, i)
14+
time.Sleep(time.Second)
15+
}
16+
}()
17+
18+
return c
19+
}

ping_pong.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"time"
6+
)
7+
8+
type Ball struct {
9+
hits int
10+
}
11+
12+
func PingPong() {
13+
table := make(chan *Ball)
14+
over := make(chan bool)
15+
go player("ping", table, over)
16+
go player("pong", table, over)
17+
18+
table <- new(Ball)
19+
time.Sleep(time.Second)
20+
over <- true
21+
over <- true
22+
}
23+
24+
func player(name string, table chan *Ball, over <-chan bool) {
25+
for {
26+
select {
27+
case ball := <-table:
28+
fmt.Println(name, ball.hits)
29+
ball.hits += 1
30+
time.Sleep(time.Millisecond * 100)
31+
table <- ball
32+
case <-over:
33+
return
34+
}
35+
}
36+
}

receive_till_quit.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func BoringReceiveTillQuit(input <-chan string, quit <-chan bool) {
6+
for {
7+
select {
8+
case s := <-input: fmt.Print(s)
9+
case <-quit:
10+
fmt.Print("Oh ok that was nice talk")
11+
return
12+
}
13+
}
14+
}

send_when_available.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"time"
6+
)
7+
8+
type Request struct {
9+
query string
10+
data chan string
11+
}
12+
13+
func SendWhenAvailable() chan<-Request {
14+
r := make(chan Request)
15+
16+
go func(r <-chan Request) {
17+
for {
18+
request := <-r
19+
time.Sleep(time.Millisecond * 100)
20+
request.data <- fmt.Sprintf("%s is a very intresting question indeed!", request.query)
21+
}
22+
}(r)
23+
24+
return r
25+
}

sequential_fan_in.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package main
2+
3+
import "fmt"
4+
5+
type Message struct {
6+
msg string
7+
wait chan bool
8+
}
9+
10+
func BoringSyncedGenerator(msg string) <-chan Message {
11+
m := make(chan Message)
12+
13+
go func() {
14+
for i := 0; ; i++ {
15+
wait := make(chan bool)
16+
m <- Message{msg: fmt.Sprintf("%s %d", msg, i), wait: wait}
17+
<-wait
18+
}
19+
}()
20+
return m
21+
}
22+
23+
func BoringSequentialFanIn(syncedChans... <-chan Message) <-chan string {
24+
c := make(chan string)
25+
26+
go func() {
27+
var msgs []Message
28+
29+
for _, v := range syncedChans {
30+
msgs = append(msgs, <-v)
31+
}
32+
33+
for _, v := range msgs {
34+
c <- v.msg
35+
}
36+
37+
for _, v := range msgs {
38+
v.wait <- true
39+
}
40+
}()
41+
42+
return c
43+
}

0 commit comments

Comments
 (0)