Last active
September 14, 2017 15:20
-
-
Save vladimirvivien/da31ec074199b921a5f8383b7f0d9d53 to your computer and use it in GitHub Desktop.
This file contains 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
type chanWriter struct { | |
ch chan byte | |
} | |
func newChanWriter() *chanWriter { | |
return &chanWriter{make(chan byte, 1024)} | |
} | |
func (w *chanWriter) Chan() <-chan byte { | |
return w.ch | |
} | |
func (w *chanWriter) Write(p []byte) (int, error) { | |
n := 0 | |
for _, b := range p { | |
w.ch <- b | |
n++ | |
} | |
return n, nil | |
} | |
func (w *chanWriter) Close() error { | |
close(w.ch) | |
return nil | |
} | |
func main() { | |
writer := newChanWriter() | |
go func() { | |
defer writer.Close() | |
writer.Write([]byte("Stream ")) | |
writer.Write([]byte("me!")) | |
}() | |
for c := range writer.Chan() { | |
fmt.Printf("%c", c) | |
} | |
fmt.Println() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment