-
Notifications
You must be signed in to change notification settings - Fork 0
/
file-store.go
44 lines (38 loc) · 861 Bytes
/
file-store.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
package main
import (
"fmt"
"log"
"os"
)
type FileStore struct {
storeDir string
}
func NewFileStore(dir string) *FileStore {
return &FileStore{
storeDir: dir,
}
}
func (fs *FileStore) PutValue(key, value string) (err error) {
filePath := fmt.Sprintf("%s/%s", fs.storeDir, key)
file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return
}
defer file.Close()
_, err = file.Write([]byte(value))
if err != nil {
return
}
log.Printf("successfully put a value file=%s, value=%s", filePath, value)
return
}
func (fs *FileStore) GetValue(key string) (result string, err error) {
filePath := fmt.Sprintf("%s/%s", fs.storeDir, key)
buf, err := os.ReadFile(filePath)
if err != nil {
return
}
result = string(buf)
log.Printf("got a value from the file=%s, value=%s", filePath, result)
return
}