1 Star 0 Fork 0

qw_1215/glink

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
fs.go 1.03 KB
一键复制 编辑 原始数据 按行查看 历史
qw_1215 提交于 2020-03-12 22:26 +08:00 . 项目初始化
package source
import (
"bufio"
streams "gitee.com/qw_1215/glink"
"gitee.com/qw_1215/glink/flow"
"os"
)
// FileSource streams data from file system
type FileSource struct {
fileName string
in chan interface{}
}
// NewFileSource returns new FileSource instance
func NewFileSource(fileName string) *FileSource {
source := &FileSource{fileName, make(chan interface{})}
source.init()
return source
}
func (fs *FileSource) init() {
go func() {
file, err := os.Open(fs.fileName)
streams.Check(err)
defer file.Close()
reader := bufio.NewReader(file)
for {
l, isPrefix, err := reader.ReadLine()
if err != nil {
close(fs.in)
break
}
var msg string
if isPrefix {
msg = string(l[:])
} else {
msg = string(l[:]) + "\n"
}
fs.in <- msg
}
}()
}
// Via streams data through given flow
func (fs *FileSource) Via(_flow streams.Flow) streams.Flow {
flow.DoStream(fs, _flow)
return _flow
}
// Out returns channel for sending data
func (fs *FileSource) Out() <-chan interface{} {
return fs.in
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/qw_1215/glink.git
git@gitee.com:qw_1215/glink.git
qw_1215
glink
glink
195e12e86392

搜索帮助