-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolid.srp.go
77 lines (57 loc) · 1.25 KB
/
solid.srp.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
package main
import (
"fmt"
"io/ioutil"
"net/url"
"strings"
)
var entryCount = 0
type Journal struct {
entries []string
}
func (j *Journal) String() string {
return strings.Join(j.entries, "\n")
}
func (j *Journal) AddEntry(text string) int {
entryCount++
entry := fmt.Sprintf("%d: %s",
entryCount,
text)
j.entries = append(j.entries, entry)
return entryCount
}
func (j *Journal) RemoveEntry(index int) {
// ...
}
// breaks srp
func (j *Journal) Save(filename string) {
_ = ioutil.WriteFile(filename,
[]byte(j.String()), 0644)
}
func (j *Journal) Load(filename string) {
}
func (j *Journal) LoadFromWeb(url *url.URL) {
}
var lineSeparator = "\n"
func SaveToFile(j *Journal, filename string) {
_ = ioutil.WriteFile(filename,
[]byte(strings.Join(j.entries, lineSeparator)), 0644)
}
type Persistence struct {
lineSeparator string
}
func (p *Persistence) saveToFile(j *Journal, filename string) {
_ = ioutil.WriteFile(filename,
[]byte(strings.Join(j.entries, p.lineSeparator)), 0644)
}
func main() {
j := Journal{}
j.AddEntry("I cried today.")
j.AddEntry("I ate a bug")
fmt.Println(strings.Join(j.entries, "\n"))
// separate function
SaveToFile(&j, "journal-s.txt")
//
p := Persistence{"\n"}
p.saveToFile(&j, "journal-p.txt")
}