-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanfile.go
72 lines (62 loc) · 1.35 KB
/
scanfile.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"
"fmt"
//"log"
"os"
"strings"
"net/http"
)
// readLines reads a whole file into memory
// and returns a slice of its lines.
func readLines(path string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}
// writeLines writes the lines to the given file.
func writeLines(lines []string, path string) error {
file, err := os.Create(path)
if err != nil {
return err
}
defer file.Close()
w := bufio.NewWriter(file)
for _, line := range lines {
fmt.Fprintln(w, line)
}
return w.Flush()
}
var lines, err = readLines("stdmsg.txt")
func main() {
http.HandleFunc("/query", FindMsg)
http.ListenAndServe(":8080",nil)
}
func lookUp(stdMsgNo string) ([]string){
var results []string
found := false
for _, line := range lines {
if strings.Contains(line,stdMsgNo){
results = append(results, line)
found =true
}
}
if found==false{
results = append(results, "Not found")
}
return results
}
func FindMsg(w http.ResponseWriter, r *http.Request){
stdMsgs := lookUp(r.URL.Query().Get("msgNo"))
for _, stdMsg := range stdMsgs {
fmt.Fprintln(w,stdMsg)
}
}