-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReadFileAndMatchText.go
90 lines (72 loc) · 1.77 KB
/
ReadFileAndMatchText.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"encoding/json"
"fmt"
"io"
"os"
"strings"
)
type xkcd struct {
Num int `json:"num"`
Day string `json:"day"`
Month string `json:"month"`
Year string `json:"year"`
Title string `json:"title"`
Transcript string `json:"transcript"`
}
func main() {
if len(os.Args) < 2 {
fmt.Fprintln(os.Stderr, "No file given")
os.Exit(-1)
}
if len(os.Args) < 3 {
fmt.Fprintln(os.Stderr, "No search text provided")
os.Exit(0)
}
var (
items []xkcd
// terms []string
input io.ReadCloser
count int
err error
)
fmt.Fprintf(os.Stderr, "Arguments of reading file %v", os.Args[2:])
// Code to read small files in from disk line by line
input, err = os.Open("comic.json")
if err != nil {
fmt.Fprintln(os.Stderr, "Error reading file", err)
return
}
decoder := json.NewDecoder(input) // Use a JSON decoder for the ReadCloser
err = decoder.Decode(&items)
if err != nil {
fmt.Fprintln(os.Stderr, "Error reading file", err)
return
}
matchedData := matchTextWithData(os.Args[2:], items, &count)
//Code to read small files in from disk
// data, err := os.ReadFile("comic.json")
// if err != nil {
// fmt.Fprintln(os.Stderr, "Error reading file", err)
// return
// }
// err = json.Unmarshal(data, &items)
// if err != nil {
// fmt.Fprintln(os.Stderr, "Error marshalig file", err)
// return
// }
fmt.Fprintf(os.Stderr, "found data is %#v and count is %d", matchedData, count)
fmt.Fprintf(os.Stderr, "found %d comic", count)
}
func matchTextWithData(text []string, data []xkcd, count *int) []xkcd {
var matched []xkcd
for _, element := range text {
for _, comic := range data {
if strings.Contains(comic.Title, element) {
matched = append(matched, comic)
*count++
}
}
}
return matched
}