SimpleFiles

package module
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2025 License: MIT Imports: 7 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.
	// Use nil for default file mode.
	f, err := SimpleFiles.New("test.json", nil)
	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.
	// Use nil for default file mode.
	f, err := SimpleFiles.New("test.json", nil)
	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 {
	// contains filtered or unexported fields
}

func New

func New(name string, perm *os.FileMode) (*File, error)

func (*File) Append added in v1.1.0

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

func (*File) AppendString added in v1.1.0

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

func (*File) Read

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

func (*File) ReadJSON

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

func (*File) ReadString

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

func (*File) ReadXML

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

func (*File) ReadYAML added in v1.1.0

func (f *File) ReadYAML(v interface{}) error

func (*File) Rename added in v1.1.0

func (f *File) Rename(newName string) error

func (*File) Write

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

func (*File) WriteJSON

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

func (*File) WriteString

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

func (*File) WriteXML

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

func (*File) WriteYAML added in v1.1.0

func (f *File) WriteYAML(v interface{}) error

func (*File) Writer added in v1.0.1

func (f *File) Writer() (io.WriteCloser, error)

Jump to

Keyboard shortcuts

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