-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_read.go
72 lines (64 loc) · 1.52 KB
/
test_read.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"bufio"
"encoding/json"
"fmt"
"os"
"runtime"
"strings"
"reflect"
)
func bToMb(b uint64) uint64 {
return b / 1024 / 1024
}
func main() {
// 打开文件
file, err := os.Open("./tmp/report.json")
if err != nil {
panic(err)
}
defer file.Close()
// 创建一个 Scanner 对象
scanner := bufio.NewScanner(file)
var m runtime.MemStats
// 逐行读取文件内容
i := 0
for scanner.Scan() {
i++
line := scanner.Text()
//fmt.Println(line)
if line == "" {
fmt.Println("space line")
continue
}
if i >= 5 {
break
}
var data map[string]interface{}
json.Unmarshal([]byte(line), &data)
if stat, ok := data["stat"].(map[string]interface{}); ok {
data["file_size"] = stat["size"]
data["file_last_access_time"] = stat["atime"]
if mode, ok := stat["mode"].(float64); ok {
fmt.Printf("%v\n", mode)
data["file_access"] = fmt.Sprintf("%d", int(mode))
}
}
delete(data, "stat")
data["dev_path"] = data["file"]
delete(data, "file")
if md5Data, ok := data["md"].(string); ok {
data["md5"] = strings.ToLower(md5Data)
delete(data, "md")
}
data["ss"] = "test"
jsonStr, _ := json.Marshal(data)
fmt.Printf("%v %s\n", string(jsonStr), reflect.TypeOf(jsonStr))
runtime.ReadMemStats(&m)
fmt.Printf("Alloc = %v B\n", m.Alloc)
}
// 检查是否有错误发生
if err := scanner.Err(); err != nil {
panic(err)
}
}