-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilies.go
55 lines (45 loc) · 1016 Bytes
/
utilies.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
package main
import (
"encoding/json"
"errors"
"log"
"os"
)
func LoadJSON() SalDat {
// Check if data.json exists
if _, err := os.Stat("data.json"); errors.Is(err, os.ErrNotExist) {
// Create data.json if not exists
rJS, err := json.MarshalIndent(SalDat{}, "", " ")
if err != nil {
panic(err)
}
os.WriteFile("data.json", rJS, 0644)
} else if err != nil {
log.Fatalf("Unknown Error: %s", err)
}
// Read JSON
file, err1 := os.ReadFile("data.json")
if err1 != nil {
log.Fatalf("Read Error: %s", err1)
}
// Map JSON to Struct
data := SalDat{}
err := json.Unmarshal([]byte(file), &data)
if err != nil {
log.Printf("Unmarshal Error: %s", err)
}
return data
}
func WriteJSON(data SalDat) SalDat {
// Struct to JSON Map
respJSON, err := json.MarshalIndent(data, "", " ")
if err != nil {
log.Printf("Marshal Error: %s", err)
}
// Write to File
err = os.WriteFile("data.json", respJSON, 0644)
if err != nil {
log.Printf("Write Error: %s", err)
}
return data
}