思考

思想就是想将整个程序分为3部分分别为读取模块,处理模块,写入模块,中间数据通过协程进行处理代码如下这是之前的代码

package main
import (
"fmt"
"strings"
"time"
)
type LogProcess struct {
rc chan string
wc chan string
path string
influshDB string
}
func (l *LogProcess) ReadFromFile(){
//读取模块
message:="this is message"
l.rc<-message
}
func (l *LogProcess) Process(){
text:=<-l.rc
res:=strings.ToUpper(text)
l.wc<-res
}
func (l *LogProcess)WriteToDB(){
//写入模块
fmt.Println(<-l.wc)
}
func main(){
lp:=LogProcess{
rc:make(chan string),
wc:make(chan string),
path: "./111.log",
influshDB: "./111.log",
}
go lp.ReadFromFile()
go lp.Process()//处理
go lp.WriteToDB()

time.Sleep(1*time.Second)
}
//缺点 第一:结构单一,获取数据方式很多。不利于扩展,所以想到了go里面的接口改进如下

改进版

package main
import (
"bufio"
"fmt"
"io"
"log"
"os"
"strings"
"time"
)
type LogProcess struct {
rc chan string
wc chan string
read Reader
write Writer
path string
file string
}
type Reader interface {
read(rc chan string)
}
type Writer interface {
write(wc chan string)
}
type ReadFromFile struct {//实现Reader接口将来如果别的获取数据方式可以做扩展
path string
}
type WriterDB struct {
dBinfo string
}
func (r *ReadFromFile) read(rc chan string){
fd,err:=os.Open(r.path)
if err != nil {
log.Println("open file is fail")
return
}
reader:=bufio.NewReader(fd)
for {
line,err:=reader.ReadBytes('\n')
if err == io.EOF {
time.Sleep(500 * time.Second)
continue
}
if err != nil {
panic(fmt.Sprintf("read is error %s",err.Error()))
}
line_str:= line[:len(line)-1]
rc <-string(line_str)
}
}
//处理
func (l *LogProcess) Process(){
for v:=range l.rc {
res:=strings.ToUpper(v)
l.wc<-res
}
}
func (w *WriterDB) write(wc chan string){
for v:=range wc {
fmt.Println(v)
}
}
func main(){
rc:=&ReadFromFile{
path:"./test.log",
}
wc:=&WriterDB{}
l:= &LogProcess{
rc:make(chan string),
wc:make(chan string),
read: rc,
write: wc,
}
go l.read.read(l.rc)
go l.Process()
go l.write.write(l.wc)
time.Sleep(10*time.Second)
}