write channel打印在输出控制台没有具体的字符串
package main
import (
"strings"
"time"
"fmt"
"os"
"bufio"
"io"
)
type Reader interface {
Read(rc chan []byte)
}
type Writer interface{
Write(wc chan string)
}
type LogProcessOpt struct {
rc chan []byte
wc chan string
read Reader
write Writer
}
type ReadFromFile struct {
path string
}
func (r *ReadFromFile) Read (rc chan []byte){
//读取模块
//打开文件
//fileName :="acc.txt";
f, err := os.Open(r.path)
if err != nil {
panic(err)
}
result , err := f.Seek(0,2)
fmt.Println(result)
buf := bufio.NewReader(f)
for {
line,err := buf.ReadBytes('\n')
if err ==io.EOF{
time.Sleep(5*time.Second)
continue
} else if err!= nil{
panic(fmt.Sprintf("ReadBytes error:%s",err.Error()))
}
rc<-line[:len(line)-1]
}
}
type WriteToInfluxDB struct {
influxDBDsn string //influx data source
}
func (w *WriteToInfluxDB) Write(wc chan string){
//写入模块
for v := range wc {
fmt.Println(v)
}
}
func (lp *LogProcessOpt) Process(){
for v := range lp.rc {
lp.wc <- strings.ToUpper(string(v[:]))
}
fmt.Println(lp.wc)
}
func main(){
r := &ReadFromFile {
path:"./acc.txt",
}
w := &WriteToInfluxDB{
influxDBDsn:"username&password..",
}
lp:= &LogProcessOpt{
rc: make(chan []byte),
wc: make(chan string),
read:r,
write:w,
}
//(*lp).ReadFromFile , go 自动处理成下面这样
go lp.read.Read(lp.rc)
go lp.Process()
go lp.write.Write(lp.wc)
time.Sleep(30*time.Second)
}
代码写法照着视频写的,当新添加文本内容到txt文件中的时候不能打印出具体的字符串