-
Notifications
You must be signed in to change notification settings - Fork 0
/
dmp.go
43 lines (35 loc) · 967 Bytes
/
dmp.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
package blobstore
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
)
// Store struct to file
func WriteObjectToFile(path string, object interface{}) error {
b, err := json.Marshal(object)
if err != nil {
return fmt.Errorf("Unable to marshall object to json: %s", err.Error())
}
file, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
return fmt.Errorf("Unable to open file path with %s: %s", path, err.Error())
}
defer file.Close()
_, err = file.Write(b)
if err != nil {
return fmt.Errorf("Unable to write byte array to file: %s", err.Error())
}
return nil
}
// Load file to struct
func LoadFileToObject(path string, object interface{}) error {
b, err := ioutil.ReadFile(path)
if err != nil {
return fmt.Errorf("Unable to open file path with %s: %s", path, err.Error())
}
if err := json.Unmarshal(b, object); err != nil {
return fmt.Errorf("Unable to decode file to struct: %s", err.Error())
}
return nil
}