SimpleFiles

package module
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 16, 2022 License: MIT Imports: 6 Imported by: 0

README

SimpleFiles

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)
		}
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type File

type File struct {
	Lock *sync.RWMutex
	// contains filtered or unexported fields
}

func New

func New(name string) (*File, error)

func (*File) Read

func (f *File) Read() ([]byte, error)

func (*File) ReadJSON

func (f *File) ReadJSON(object interface{}) error

func (*File) ReadString

func (f *File) ReadString() (string, error)

func (*File) ReadXML

func (f *File) ReadXML(object interface{}) error

func (*File) Write

func (f *File) Write(b []byte) error

func (*File) WriteJSON

func (f *File) WriteJSON(object interface{}) error

func (*File) WriteString

func (f *File) WriteString(s string) error

func (*File) WriteXML

func (f *File) WriteXML(object interface{}) error

func (*File) Writer added in v1.0.1

func (f *File) Writer() io.Writer

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL