gl01/internal/util/json.go

41 lines
528 B
Go
Raw Permalink Normal View History

2022-02-05 19:43:28 +08:00
package util
import (
"encoding/json"
"os"
)
func LoadJSON(filename string, object interface{}) (err error) {
file, err := os.Open(filename)
if err != nil {
return
}
dec := json.NewDecoder(file)
err = dec.Decode(object)
if err != nil {
return
}
file.Close()
return
}
func SaveJSON(filename string, object interface{}) (err error) {
file, err := os.Create(filename)
if err != nil {
return
}
enc := json.NewEncoder(file)
err = enc.Encode(object)
if err != nil {
return
}
file.Close()
return
}