-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
75 lines (63 loc) · 1.09 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"fmt"
"os"
)
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 producer(w *chanWriter) {
defer w.Close()
i := 0
for {
w.Write([]byte(fmt.Sprint(i)))
w.Write([]byte(" - Stream "))
w.Write([]byte("me "))
w.Write([]byte("PLEAAASE!\n"))
w.Write([]byte("PLEAAASE!\n"))
//time.Sleep(2 * time.Second)
i++
}
}
func main() {
// Creates file
file, err := os.Create("./streaming")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer file.Close()
writer := newChanWriter()
go producer(writer)
for c := range writer.Chan() {
val := []byte{c}
n, err := file.Write(val)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if n != len(val) {
fmt.Println("failed to write data")
os.Exit(1)
}
}
fmt.Println("file write done")
}