write channel打印在输出控制台没有具体的字符串

来源:-

慕粉2135548801

2018-09-01 16:36

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文件中的时候不能打印出具体的字符串

https://img.mukewang.com/5b8a4f6600011b2905560081.jpg


写回答 关注

0回答

还没有人回答问题,可以看看其他问题

Go并发编程案例解析

课程带你通过一个真实的线上日志监控系统学习Golang以及并发的编程思想。

15219 学习 · 53 问题

查看课程

相似问题