func (f *File) Update(id string, updatedReq Resource) (Resource, error) {
	data, err := readFile(f.filename)
	if err != nil {
		return nil, err
	}
	if err = checkResourceKeyExists(data, f.key); err != nil {
		return nil, ErrResourceNotFound
	}
	updated, err := f.FindById(id)
	if err != nil {
		return nil, err
	}
	for key, val := range updatedReq {
		updated[key] = val
	}
	updated["id"] = id
	newResources := make([]Resource, 0)
	for _, d := range data[f.key] {
		if d["id"] == id {
			newResources = append(newResources, updated)
		} else {
			newResources = append(newResources, d)
		}
	}
	data[f.key] = newResources
	if err := updateFile(f.filename, data); err != nil {
		return nil, err
	}
	return updated, nil
}
Update an existing resource for the specific key.
package storage
import (
	"encoding/json"
	"errors"
	"io/ioutil"
	"math/rand"
	"strconv"
)
var (
	errResourceInvalidType = errors.New("invalid resource type")
)
type File struct {
	filename string
	key      string
}
func NewFile(filename, key string) (*File, error) {
	return &File{filename: filename, key: key}, nil
}
func (f *File) Find() ([]Resource, error) {
	data, err := readFile(f.filename)
	if err != nil {
		return nil, err
	}
	if err = checkResourceKeyExists(data, f.key); err != nil {
		return nil, ErrResourceNotFound
	}
	return data[f.key], nil
}
func (f *File) FindById(id string) (Resource, error) {
	data, err := readFile(f.filename)
	if err != nil {
		return nil, err
	}
	if err = checkResourceKeyExists(data, f.key); err != nil {
		return nil, ErrResourceNotFound
	}
	for _, resource := range data[f.key] {
		if resource["id"] == id {
			return resource, nil
		}
	}
	return nil, ErrResourceNotFound
}
func (f *File) Create(newResource Resource) (Resource, error) {
	data, err := readFile(f.filename)
	if err != nil {
		return nil, err
	}
	if err = checkResourceKeyExists(data, f.key); err != nil {
		return nil, ErrResourceNotFound
	}
	_, ok := newResource["id"]
	if !ok {
		newResource["id"] = generateNewId(data[f.key])
	} else {
		for _, resource := range data[f.key] {
			if resource["id"] == newResource["id"] {
				return nil, ErrResourceAlreadyExists
			}
		}
	}
	newData := append(data[f.key], newResource)
	data[f.key] = newData
	if err := updateFile(f.filename, data); err != nil {
		return nil, err
	}
	return newResource, nil
}
func (f *File) Replace(id string, replaced Resource) (Resource, error) {
	data, err := readFile(f.filename)
	if err != nil {
		return nil, err
	}
	if err = checkResourceKeyExists(data, f.key); err != nil {
		return nil, ErrResourceNotFound
	}
	if _, err = f.FindById(id); err != nil {
		return nil, err
	}
	replaced["id"] = id
	newResources := make([]Resource, 0)
	for _, d := range data[f.key] {
		if d["id"] == id {
			newResources = append(newResources, replaced)
		} else {
			newResources = append(newResources, d)
		}
	}
	data[f.key] = newResources
	if err := updateFile(f.filename, data); err != nil {
		return nil, err
	}
	return replaced, nil
}