-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
56 lines (47 loc) · 952 Bytes
/
file.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
package LogAnalysis
import (
"bufio"
"io"
"log"
"os"
)
type FileControl struct {
//path
Path []string
LineChan chan []byte
}
// var file FileControl
//
// func init() {
// //初始化他的chan
// }
func (f FileControl) Init() {
//获取文本,并且一行一行的读入数据
for i := 0; i < len(f.Path); i++ {
go f.transferChan(f.Path[i])
}
//最后才把通知报错的关闭 。或者不关也行,防止select没跑完就被关闭了
}
func (f FileControl) transferChan(path string) {
file, err := os.OpenFile(path, os.O_RDONLY, 0666)
if err != nil {
log.Println("open file is failure err :", err)
}
defer file.Close()
reader := bufio.NewReader(file)
for {
line, _, err := reader.ReadLine()
if err != nil {
log.Println(err)
if err == io.EOF {
f.LineChan <- []byte{'E', 'O', 'F'}
}
return
}
//到末尾了
if len(line) > 0 {
//log.Println(line, end)
f.LineChan <- line
}
}
}