package children import ( "encoding/json" "errors" "os" "sync" "mcquay.me/fs" ) var mut sync.Mutex type DB map[string]int func Load(filename string) (children DB, err error) { db := DB{} if !fs.Exists(filename) { return db, errors.New("db file doesn't exist") } f, err := os.Open(filename) if err != nil { return nil, err } if err := json.NewDecoder(f).Decode(&db); err != nil { return nil, err } return db, nil } func (db DB) Save(filename string) error { mut.Lock() defer mut.Unlock() f, err := os.Create(filename) if err != nil { return err } defer f.Close() if err := json.NewEncoder(f).Encode(db); err != nil { return err } return nil }