file is a wrapper for an os.File which adds support for file locking.
File*os.File
// os specific
// whether file is opened for appending
// nil unless directory being read
File.file.namestring
// whether we set nonblocking mode
File.file.pfdpoll.FD
// whether this is stdout or stderr
msync.Mutex
Chdir changes the current working directory to the file,
which must be a directory.
If there is an error, it will be of type *PathError.
Chmod changes the mode of the file to mode.
If there is an error, it will be of type *PathError.
Chown changes the numeric uid and gid of the named file.
If there is an error, it will be of type *PathError.
On Windows, it always returns the syscall.EWINDOWS error, wrapped
in *PathError.
Close closes the File, rendering it unusable for I/O.
On files that support SetDeadline, any pending I/O operations will
be canceled and return immediately with an error.
Close will return an error if it has already been called.
Fd returns the integer Unix file descriptor referencing the open file.
If f is closed, the file descriptor becomes invalid.
If f is garbage collected, a finalizer may close the file descriptor,
making it invalid; see runtime.SetFinalizer for more information on when
a finalizer might be run. On Unix systems this will cause the SetDeadline
methods to stop working.
Because file descriptors can be reused, the returned file descriptor may
only be closed through the Close method of f, or by its finalizer during
garbage collection. Otherwise, during garbage collection the finalizer
may close an unrelated file descriptor with the same (reused) number.
As an alternative, see the f.SyscallConn method.
(*T) Lock() error
Name returns the name of the file as presented to Open.
Read reads up to len(b) bytes from the File.
It returns the number of bytes read and any error encountered.
At end of file, Read returns 0, io.EOF.
ReadAt reads len(b) bytes from the File starting at byte offset off.
It returns the number of bytes read and the error, if any.
ReadAt always returns a non-nil error when n < len(b).
At end of file, that error is io.EOF.
ReadDir reads the contents of the directory associated with the file f
and returns a slice of DirEntry values in directory order.
Subsequent calls on the same file will yield later DirEntry records in the directory.
If n > 0, ReadDir returns at most n DirEntry records.
In this case, if ReadDir returns an empty slice, it will return an error explaining why.
At the end of a directory, the error is io.EOF.
If n <= 0, ReadDir returns all the DirEntry records remaining in the directory.
When it succeeds, it returns a nil error (not io.EOF).
ReadFrom implements io.ReaderFrom.
Readdir reads the contents of the directory associated with file and
returns a slice of up to n FileInfo values, as would be returned
by Lstat, in directory order. Subsequent calls on the same file will yield
further FileInfos.
If n > 0, Readdir returns at most n FileInfo structures. In this case, if
Readdir returns an empty slice, it will return a non-nil error
explaining why. At the end of a directory, the error is io.EOF.
If n <= 0, Readdir returns all the FileInfo from the directory in
a single slice. In this case, if Readdir succeeds (reads all
the way to the end of the directory), it returns the slice and a
nil error. If it encounters an error before the end of the
directory, Readdir returns the FileInfo read until that point
and a non-nil error.
Most clients are better served by the more efficient ReadDir method.
Readdirnames reads the contents of the directory associated with file
and returns a slice of up to n names of files in the directory,
in directory order. Subsequent calls on the same file will yield
further names.
If n > 0, Readdirnames returns at most n names. In this case, if
Readdirnames returns an empty slice, it will return a non-nil error
explaining why. At the end of a directory, the error is io.EOF.
If n <= 0, Readdirnames returns all the names from the directory in
a single slice. In this case, if Readdirnames succeeds (reads all
the way to the end of the directory), it returns the slice and a
nil error. If it encounters an error before the end of the
directory, Readdirnames returns the names read until that point and
a non-nil error.
Seek sets the offset for the next Read or Write on file to offset, interpreted
according to whence: 0 means relative to the origin of the file, 1 means
relative to the current offset, and 2 means relative to the end.
It returns the new offset and an error, if any.
The behavior of Seek on a file opened with O_APPEND is not specified.
If f is a directory, the behavior of Seek varies by operating
system; you can seek to the beginning of the directory on Unix-like
operating systems, but not on Windows.
SetDeadline sets the read and write deadlines for a File.
It is equivalent to calling both SetReadDeadline and SetWriteDeadline.
Only some kinds of files support setting a deadline. Calls to SetDeadline
for files that do not support deadlines will return ErrNoDeadline.
On most systems ordinary files do not support deadlines, but pipes do.
A deadline is an absolute time after which I/O operations fail with an
error instead of blocking. The deadline applies to all future and pending
I/O, not just the immediately following call to Read or Write.
After a deadline has been exceeded, the connection can be refreshed
by setting a deadline in the future.
If the deadline is exceeded a call to Read or Write or to other I/O
methods will return an error that wraps ErrDeadlineExceeded.
This can be tested using errors.Is(err, os.ErrDeadlineExceeded).
That error implements the Timeout method, and calling the Timeout
method will return true, but there are other possible errors for which
the Timeout will return true even if the deadline has not been exceeded.
An idle timeout can be implemented by repeatedly extending
the deadline after successful Read or Write calls.
A zero value for t means I/O operations will not time out.
SetReadDeadline sets the deadline for future Read calls and any
currently-blocked Read call.
A zero value for t means Read will not time out.
Not all files support setting deadlines; see SetDeadline.
SetWriteDeadline sets the deadline for any future Write calls and any
currently-blocked Write call.
Even if Write times out, it may return n > 0, indicating that
some of the data was successfully written.
A zero value for t means Write will not time out.
Not all files support setting deadlines; see SetDeadline.
Stat returns the FileInfo structure describing file.
If there is an error, it will be of type *PathError.
Sync commits the current contents of the file to stable storage.
Typically, this means flushing the file system's in-memory copy
of recently written data to disk.
SyscallConn returns a raw file.
This implements the syscall.Conn interface.
Truncate changes the size of the file.
It does not change the I/O offset.
If there is an error, it will be of type *PathError.
(*T) Unlock() error
Write writes len(b) bytes to the File.
It returns the number of bytes written and an error, if any.
Write returns a non-nil error when n != len(b).
WriteAt writes len(b) bytes to the File starting at byte offset off.
It returns the number of bytes written and an error, if any.
WriteAt returns a non-nil error when n != len(b).
If file was opened with the O_APPEND flag, WriteAt returns an error.
WriteString is like Write, but writes the contents of string s rather than
a slice of bytes.
checkValid checks whether f is valid for use.
If not, it returns an appropriate error, perhaps incorporating the operation name op.
See docs in file.go:(*File).Chmod.
( T) close() error
pread reads len(b) bytes from the File starting at byte offset off.
It returns the number of bytes read and the error, if any.
EOF is signaled by a zero count with err set to nil.
pwrite writes len(b) bytes to the File starting at byte offset off.
It returns the number of bytes written and an error, if any.
read reads up to len(b) bytes from the File.
It returns the number of bytes read and an error, if any.
( T) readFrom(r io.Reader) (n int64, handled bool, err error)( T) readdir(n int, mode os.readdirMode) (names []string, dirents []os.DirEntry, infos []os.FileInfo, err error)
seek sets the offset for the next Read or Write on file to offset, interpreted
according to whence: 0 means relative to the origin of the file, 1 means
relative to the current offset, and 2 means relative to the end.
It returns the new offset and an error, if any.
setDeadline sets the read and write deadline.
setReadDeadline sets the read deadline.
setWriteDeadline sets the write deadline.
wrapErr wraps an error that occurred during an operation on an open file.
It passes io.EOF through unchanged, otherwise converts
poll.ErrFileClosing to ErrClosed and wraps the error in a PathError.
write writes len(b) bytes to the File.
It returns the number of bytes written and an error, if any.
*T : github.com/go-git/go-billy/v5.File
T : github.com/go-git/go-git/v5/plumbing/protocol/packp/sideband.Progress
T : github.com/jbenet/go-context/io.Reader
T : github.com/jbenet/go-context/io.Writer
T : io.Closer
T : io.ReadCloser
T : io.Reader
T : io.ReaderAt
T : io.ReaderFrom
T : io.ReadSeekCloser
T : io.ReadSeeker
T : io.ReadWriteCloser
T : io.ReadWriter
T : io.ReadWriteSeeker
T : io.Seeker
T : io.StringWriter
T : io.WriteCloser
T : io.Writer
T : io.WriterAt
T : io.WriteSeeker
T : io/fs.File
T : io/fs.ReadDirFile
T : mime/multipart.File
T : net/http.File
T : syscall.Conn
T : golang.org/x/net/http2.stringWriter
T : net/http.http2stringWriter
Package-Level Functions (total 2, in which 1 are exported)
The pages are generated with Goldsv0.3.2-preview. (GOOS=darwin GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds.