Created
October 20, 2016 16:27
-
-
Save jooyunghan/195022db91523f8b9d7d06ef642a5f08 to your computer and use it in GitHub Desktop.
// cspbook 4.4 X2 UNPACK >> PACK
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" | |
// cspbook 4.4 X2 UNPACK >> PACK | |
func unpack(in <-chan string, out chan<- rune) { | |
for s := range in { | |
for _, c := range s { | |
out <- c | |
} | |
} | |
close(out) | |
} | |
func pack(in <-chan rune, out chan<- string) { | |
done := false | |
for !done { | |
s := "" | |
for c := range in { | |
s = s + string(c) | |
if len(s) == 5 { | |
break | |
} | |
} | |
out <- s | |
done = len(s) < 5 | |
} | |
close(out) | |
} | |
func main() { | |
c1 := make(chan string) | |
c2 := make(chan rune) | |
c3 := make(chan string) | |
go unpack(c1, c2) | |
go pack(c2, c3) | |
go func() { | |
c1 <- "1234" | |
c1 <- "1234" | |
c1 <- "1234" | |
close(c1) | |
} () | |
for c := range c3 { | |
fmt.Printf("%q \n", c) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment