ut/utio/file.go

36 lines
855 B
Go

package utio
import (
"os"
)
// DecodeFile tries to open the file at the specified path and if successful it decodes d.
func DecodeFile(path string, d Decoder) error {
return ReadFile(path, d.Decode)
}
// ReadFile tries to open the file at the specified path and if successful it invokes fn.
func ReadFile(path string, fn ReadFunc) error {
var f, err = os.Open(path)
if nil != err {
return err
}
defer f.Close()
return fn(f)
}
// EncodeFile tries to open the file at the specified path and if successful it encodes e.
func EncodeFile(path string, e Encoder) error {
return WriteFile(path, e.Encode)
}
// WriteFile tries to create the file at the specified path and if successful it invokes fn.
func WriteFile(path string, fn WriteFunc) error {
var f, err = os.Create(path)
if nil != err {
return err
}
defer f.Close()
return fn(f)
}