-
Notifications
You must be signed in to change notification settings - Fork 1
/
gob.go
43 lines (35 loc) · 888 Bytes
/
gob.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 utils
import (
"encoding/gob"
"os"
)
// WriteGob encodes and stores data to a file.
// It takes the data to be stored and the file path as input.
// It returns an error if any.
func WriteGob(data interface{}, filePath string) error {
file, err := os.Create(filePath)
if err != nil {
return err
}
defer file.Close()
encoder := gob.NewEncoder(file)
if err = encoder.Encode(data); err != nil {
return err
}
return nil
}
// ReadGob decodes and loads data from a file into the target variable.
// It takes the file path and a pointer to the target variable as input.
// It returns an error if any.
func ReadGob(filePath string, target interface{}) error {
readFile, err := os.Open(filePath)
if err != nil {
return err
}
defer readFile.Close()
decoder := gob.NewDecoder(readFile)
if err = decoder.Decode(target); err != nil {
return err
}
return nil
}