v.1
package main
import (
"fmt"
"strings"
"time"
)
type LoadProcess struct {
readChanel chan string
writeChannel chan string
read Reader
write Writer
}
type Reader interface {
Read(readChanel chan string)
}
type Writer interface {
Write(writeChannel chan string)
}
//读取数据
type ReadFromFile struct {
path string
}
func (r *ReadFromFile) Read(readChanel chan string) {
fmt.Println("从file中读取数据")
readChanel <- "message"
}
//处理数据
func (p *LoadProcess) Process() {
fmt.Println("开始处理...")
fmt.Println("处理结束...")
str := <-p.readChanel
//放入到写入channel
p.writeChannel <- strings.ToUpper(str)
}
//写入数据
type WriteToMysql struct {
dsn string
}
func (w *WriteToMysql) Write(writeChannel chan string) {
fmt.Println("写入数据" + <-writeChannel)
}
func main() {
readFile := &ReadFromFile{"/data/www/acces.log"}
writeMysql := &WriteToMysql{"username&password"}
loadProcess := &LoadProcess{
readChanel: make(chan string),
writeChannel: make(chan string),
read: readFile,
write: writeMysql,
}
go loadProcess.read.Read(loadProcess.readChanel)
go loadProcess.Process()
go loadProcess.write.Write(loadProcess.writeChannel)
time.Sleep(time.Second * 2)
}
output:
从file中读取数据
开始处理...
处理结束...
写入数据MESSAGE
v2.0
package main
import (
"bufio"
"fmt"
"io"
"os"
"strings"
"time"
)
type LoadProcess struct {
readChanel chan string
writeChannel chan string
read Reader
write Writer
}
type Reader interface {
Read(readChanel chan string)
}
type Writer interface {
Write(writeChannel chan string)
}
//读取数据
type ReadFromFile struct {
path string
}
func (r *ReadFromFile) Read(readChanel chan string) {
f, err := os.Open(r.path)
if err != nil {
panic(err)
}
//字符指针移动到文件末尾
f.Seek(0, 2)
bufReader := bufio.NewReader(f)
for {
line, _, err := bufReader.ReadLine()
if err == io.EOF {
time.Sleep(time.Second * 2)
continue
} else if err != nil {
panic(err)
}
fmt.Println(string(line))
readChanel <- string(line)
}
}
//处理数据
func (p *LoadProcess) Process() {
//放入到写入channel
for str := range p.readChanel {
p.writeChannel <- strings.ToUpper(str)
}
}
//写入数据
type WriteToMysql struct {
dsn string
}
func (w *WriteToMysql) Write(writeChannel chan string) {
for str := range writeChannel {
fmt.Println("写入数据" + str)
}
}
func main() {
readFile := &ReadFromFile{"/Users/wenba/Desktop/awesomeProject/acces.log"}
writeMysql := &WriteToMysql{"username&password"}
loadProcess := &LoadProcess{
readChanel: make(chan string),
writeChannel: make(chan string),
read: readFile,
write: writeMysql,
}
go loadProcess.read.Read(loadProcess.readChanel)
go loadProcess.Process()
go loadProcess.write.Write(loadProcess.writeChannel)
time.Sleep(time.Second * 1200)
}