Last active
April 14, 2016 16:18
-
-
Save md2perpe/f2f5d3b91f63b403ef70bd5d5295e968 to your computer and use it in GitHub Desktop.
Building H2O - https://blog.ksub.org/bytes/2016/03/13/building-ho/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
type H struct { seq int } | |
type O struct { seq int } | |
type H2O struct { | |
h1, h2 H | |
o O | |
} | |
func (h2o H2O)String() string { | |
return fmt.Sprintf("H(%d)O(%d)H(%d)", h2o.h1.seq, h2o.o.seq, h2o.h2.seq) | |
} | |
func main() { | |
chH := make(chan H, 10) | |
chO := make(chan O, 10) | |
go func() { | |
for i:=1; ; i++ { | |
chH <- H{i} | |
} | |
}() | |
go func() { | |
for i:=1; ; i++ { | |
chO <- O{i} | |
} | |
}() | |
chH2O := make(chan H2O, 10) | |
go func() { | |
for { | |
h1 := <-chH | |
h2 := <-chH | |
o := <-chO | |
chH2O <- H2O{h1, h2, o} | |
} | |
}() | |
for i:=0; i<5; i++ { | |
h2o := <-chH2O | |
fmt.Println(h2o) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment