SimpleFiles is a go library to make reading and writing to file easier. SimpleFiles uses a RWMutex to prevent errors when using a file in multiple goroutines.
go get github.com/JGLTechnologies/SimpleFiles
Reading JSON from a file
package main
import (
"fmt"
"github.com/JGLTechnologies/SimpleFiles"
)
func main() {
var j map[string]interface{}
// If the file does not exist it will be created.
f, err := SimpleFiles.New("test.json")
if err != nil {
panic(err)
} else {
err := f.ReadJSON(&j)
if err != nil {
panic(err)
} else {
fmt.Println(j["name"])
}
}
}
Writing JSON to a file
package main
import (
"github.com/JGLTechnologies/SimpleFiles"
)
func main() {
j := map[string]interface{}{"name": "Joe", "age": 47}
// If the file does not exist it will be created.
f, err := SimpleFiles.New("test.json")
if err != nil {
panic(err)
} else {
err := f.WriteJSON(j)
if err != nil {
panic(err)
}
}
}