std/os
import "github.com/eugenioenko/goalchemy/std/os"Package os reads and writes whole files on the host file system. Errors are *PathError values whose Err matches ErrNotExist, ErrExist, ErrPermission or errors.ErrUnsupported through errors.Is, as in Go. Hosts without a file system, such as browsers, fail every call with an error matching errors.ErrUnsupported.
const ( O_RDONLY int = os.RDONLY O_WRONLY int = os.WRONLY O_RDWR int = os.RDWR O_APPEND int = os.APPEND O_CREATE int = os.CREATE O_EXCL int = os.EXCL O_TRUNC int = os.TRUNC)Flags to OpenFile, combined with bitwise or. Exactly one of O_RDONLY, O_WRONLY and O_RDWR selects the access mode.
const MaxFileBytes = os.MaxFileBytesMaxFileBytes is the largest file ReadFile returns and WriteFile accepts.
var ( ErrInvalid = errors.New("invalid argument") ErrPermission = errors.New("permission denied") ErrExist = errors.New("file already exists") ErrNotExist = errors.New("file does not exist"))Portable analogs of the io/fs errors.
var ErrClosed = errors.New("file already closed")ErrClosed is the error of an operation on a closed File, as in Go.
ReadFile
Section titled “ReadFile”func ReadFile(name string) ([]byte, error)ReadFile reads the named file and returns its contents. A directory fails with op “read”, as on Linux; other failures use op “open”.
Remove
Section titled “Remove”func Remove(name string) errorRemove removes the named file or empty directory.
Rename
Section titled “Rename”func Rename(oldpath, newpath string) errorRename renames oldpath to newpath, replacing a file at newpath. Failures are *LinkError values.
WriteFile
Section titled “WriteFile”func WriteFile(name string, data []byte, perm FileMode) errorWriteFile writes data to the named file, creating it if necessary. If the file does not exist, WriteFile creates it with permissions perm (before the umask); otherwise WriteFile truncates it before writing, without changing permissions.
type File
Section titled “type File”type File struct {}File is an open host file. Its methods implement io.Reader, io.ReaderAt, io.Writer, io.Seeker, io.StringWriter and io.Closer. A nil *File fails every method with ErrInvalid; a closed one with ErrClosed.
Create
Section titled “Create”func Create(name string) (*File, error)Create creates or truncates the named file for reading and writing with mode 0o666 before the umask.
CreateTemp
Section titled “CreateTemp”func CreateTemp(dir, pattern string) (*File, error)CreateTemp creates a new file in dir, or in the host’s temporary directory when dir is empty, opened for reading and writing with mode 0o600. Its base name is pattern with the last “*” replaced by a random string, or pattern followed by one. The caller removes the file.
func Open(name string) (*File, error)Open opens the named file for reading.
OpenFile
Section titled “OpenFile”func OpenFile(name string, flag int, perm FileMode) (*File, error)OpenFile opens the named file with the given flags and, when it creates the file, perm before the umask. Failures are *PathError values with op “open”.
File.Close
Section titled “File.Close”func (f *File) Close() errorClose closes the file. A second Close fails with ErrClosed.
File.Name
Section titled “File.Name”func (f *File) Name() stringName returns the name the file was opened with.
File.Read
Section titled “File.Read”func (f *File) Read(b []byte) (int, error)Read reads up to len(b) bytes into b and returns the count. At the end of the file it returns 0, io.EOF.
File.ReadAt
Section titled “File.ReadAt”func (f *File) ReadAt(b []byte, off int64) (int, error)ReadAt reads len(b) bytes starting at offset off, without moving the file’s offset. It returns io.EOF with the count when the file ends first.
File.Seek
Section titled “File.Seek”func (f *File) Seek(offset int64, whence int) (int64, error)Seek sets the offset for the next Read or Write relative to the start (io.SeekStart), the current offset (io.SeekCurrent) or the end (io.SeekEnd), and returns the new offset.
File.Stat
Section titled “File.Stat”func (f *File) Stat() (FileInfo, error)Stat describes the file by its name.
File.Sync
Section titled “File.Sync”func (f *File) Sync() errorSync commits the file’s written data to the storage device.
File.Write
Section titled “File.Write”func (f *File) Write(b []byte) (int, error)Write writes b at the file’s offset, or at its end when it was opened with O_APPEND, and returns the bytes written. A short write without a host failure returns io.ErrShortWrite.
File.WriteString
Section titled “File.WriteString”func (f *File) WriteString(s string) (int, error)WriteString writes s.
type FileInfo
Section titled “type FileInfo”type FileInfo interface { Name() string Size() int64 Mode() FileMode IsDir() bool}A FileInfo describes a file, as returned by Stat. Modification times are not reported.
func Stat(name string) (FileInfo, error)Stat describes the named file. Failures are *PathError values with op “stat”.
type FileMode
Section titled “type FileMode”type FileMode uint32A FileMode holds permission bits; only the low nine bits are applied.
const ( ModeDir FileMode = 1 << 31 ModePerm FileMode = 0o777)ModeDir marks a directory in a FileMode; ModePerm selects its permission bits. Other Go mode bits are not reported.
FileMode.IsDir
Section titled “FileMode.IsDir”func (m FileMode) IsDir() boolIsDir reports whether m describes a directory.
FileMode.IsRegular
Section titled “FileMode.IsRegular”func (m FileMode) IsRegular() boolIsRegular reports whether m describes a regular file.
FileMode.Perm
Section titled “FileMode.Perm”func (m FileMode) Perm() FileModePerm returns the permission bits of m.
type LinkError
Section titled “type LinkError”type LinkError struct { Op string Old string New string Err error}LinkError records an error during a Rename and the paths that caused it.
LinkError.Error
Section titled “LinkError.Error”func (e *LinkError) Error() stringLinkError.Unwrap
Section titled “LinkError.Unwrap”func (e *LinkError) Unwrap() errortype PathError
Section titled “type PathError”type PathError struct { Op string Path string Err error}PathError records an error and the operation and file path that caused it.
PathError.Error
Section titled “PathError.Error”func (e *PathError) Error() stringPathError.Unwrap
Section titled “PathError.Unwrap”func (e *PathError) Unwrap() error