Documentation ¶
Overview ¶
Package jsonfile persists a Go value to a JSON file.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type JSONFile ¶
type JSONFile[Data any] struct { // contains filtered or unexported fields }
JSONFile holds a Go value of type Data and persists it to a JSON file. Data is accessed and modified using the Read and Write methods. Create a JSONFile using the New or Load functions.
func Load ¶
Load loads an existing JSONFileData from the given path.
If the file does not exist, Load returns an error that can be checked with os.IsNotExist.
Load and New are separate to avoid creating a new file when starting a service, which could lead to data loss. To both load an existing file or create it (which you may want to do in a development environment), combine Load with New, like this:
db, err := jsonfile.Load[Data](path) if os.IsNotExist(err) { db, err = jsonfile.New[Data](path) }