2 Star 0 Fork 0

小蚂蚁/gurl

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
read_file.go 1.20 KB
Copy Edit Raw Blame History
package input
import (
"bufio"
"encoding/json"
"fmt"
"os"
"strings"
)
type StreamFile struct {
JsonOut chan string
file *os.File
}
func ReadFile(fileName string, fieldSeparator string, renameKey string) (*StreamFile, error) {
file, err := os.Open(fileName)
if err != nil {
return nil, err
}
sf := StreamFile{
JsonOut: make(chan string, 10),
file: file,
}
scanner := bufio.NewScanner(file)
renameMap := map[string]string{}
if len(renameKey) > 0 {
defaultKeys := strings.FieldsFunc(renameKey, func(r rune) bool { return r == ',' })
for _, v := range defaultKeys {
if pos := strings.Index(v, "="); pos != -1 {
renameMap[v[pos+1:]] = v[:pos]
}
}
}
go func() {
defer func() {
sf.file.Close()
close(sf.JsonOut)
}()
for scanner.Scan() {
ls := strings.Split(scanner.Text(), fieldSeparator)
m := make(map[string]string)
for k, v := range ls {
colName := fmt.Sprintf("rf.col.%d", k)
newKey, ok := renameMap[colName]
if ok {
m[newKey] = v
continue
}
m[colName] = v
}
all, err := json.Marshal(m)
if err != nil {
fmt.Printf("%s\n", err)
os.Exit(1)
}
sf.JsonOut <- string(all)
}
}()
return &sf, nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/guonaihong/gurl.git
git@gitee.com:guonaihong/gurl.git
guonaihong
gurl
gurl
v0.0.1

Search