-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatasource.go
95 lines (77 loc) · 1.53 KB
/
datasource.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
91
92
93
94
95
package main
import (
"bufio"
"fmt"
"math/rand"
"os"
"strconv"
"time"
)
func getTodaysLaw() (law string) {
laws := readLawFile("tech_laws.file")
lineToRead := getLawNumberToRead(len(laws))
return laws[lineToRead]
}
func getLawNumberToRead(max int) (lineToRead int) {
for runCount := 0; ; runCount++ {
rand.Seed(time.Now().UnixNano())
lineToRead = rand.Intn(rand.Intn(max-1+1) + 1)
statFileSlice := readStatFile()
if getPosition(statFileSlice, strconv.Itoa(lineToRead)) == -1 {
break
}
if runCount == max {
clearStatFile()
runCount = 0
}
}
writeStatFile(fmt.Sprintln(lineToRead))
return
}
func clearStatFile() {
_, err := os.Create("stat")
if err != nil {
panic(err)
}
}
func writeStatFile(input string) {
// []byte(input), 0666
f, err := os.OpenFile("stat", os.O_CREATE|os.O_APPEND, os.ModeAppend)
if err != nil {
panic(err)
}
defer f.Close()
_, werr := f.WriteString(input)
if werr != nil {
panic(werr)
}
}
func readStatFile() (dataSlice []string) {
f, err := os.Open("stat")
if err != nil {
fmt.Println("Ridi")
return nil
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
dataSlice = append(dataSlice, scanner.Text())
}
return
}
func readLawFile(fileName string) (dataMap map[int]string) {
f, err := os.Open(fileName)
if err != nil {
fmt.Println("Ridi")
return nil
}
defer f.Close()
scanner := bufio.NewScanner(f)
dataMap = make(map[int]string)
lineNumber := 0
for scanner.Scan() {
lineNumber++
dataMap[lineNumber] = scanner.Text()
}
return
}