Documentation ¶
Overview ¶
The os package provides a platform-independent interface to operating system functionality. The design is Unix-like.
Index ¶
- Constants
- Variables
- func Clearenv()
- func Environ() []string
- func Exit(code int)
- func Getegid() int
- func Getenv(key string) string
- func Geteuid() int
- func Getgid() int
- func Getpagesize() int
- func Getpid() int
- func Getppid() int
- func Getuid() int
- func Lstat(name string) (dir *Dir, err Error)
- func Open(name string, flag int, perm int) (file *File, err Error)
- func Pipe() (r *File, w *File, err Error)
- func Stat(name string) (dir *Dir, err Error)
- func Wait(pid int, options int) (w *Waitmsg, err Error)
- type Dir
- type Errno
- type Error
- func Chdir(dir string) Error
- func Chmod(name string, mode int) Error
- func Chown(name string, uid, gid int) Error
- func Exec(argv0 string, argv []string, envv []string) Error
- func ForkExec(argv0 string, argv []string, envv []string, dir string, fd []*File) (pid int, err Error)
- func Getenverror(key string) (value string, err Error)
- func Getgroups() ([]int, Error)
- func Getwd() (string, Error)
- func Hostname() (name string, err Error)
- func Lchown(name string, uid, gid int) Error
- func Link(oldname, newname string) Error
- func Mkdir(name string, perm int) Error
- func MkdirAll(path string, perm int) Error
- func NewError(s string) Error
- func NewSyscallError(syscall string, errno int) Error
- func Readlink(name string) (string, Error)
- func Remove(name string) Error
- func RemoveAll(path string) Error
- func Rename(oldname, newname string) Error
- func Setenv(key, value string) Error
- func Symlink(oldname, newname string) Error
- func Time() (sec int64, nsec int64, err Error)
- func Truncate(name string, size int64) Error
- type ErrorString
- type File
- func (f *File) Chdir() Error
- func (f *File) Chmod(mode int) Error
- func (f *File) Chown(uid, gid int) Error
- func (file *File) Close() Error
- func (file *File) Fd() int
- func (file *File) Name() string
- func (file *File) Read(b []byte) (n int, err Error)
- func (file *File) ReadAt(b []byte, off int64) (n int, err Error)
- func (file *File) Readdir(count int) (dirs []Dir, err Error)
- func (file *File) Readdirnames(count int) (names []string, err Error)
- func (file *File) Seek(offset int64, whence int) (ret int64, err Error)
- func (file *File) Stat() (dir *Dir, err Error)
- func (f *File) Truncate(size int64) Error
- func (file *File) Write(b []byte) (n int, err Error)
- func (file *File) WriteAt(b []byte, off int64) (n int, err Error)
- func (file *File) WriteString(s string) (ret int, err Error)
- type LinkError
- type PathError
- type SyscallError
- type Waitmsg
Constants ¶
const ( WNOHANG = syscall.WNOHANG // Don't wait if no process has exited. WSTOPPED = syscall.WSTOPPED // If set, status of stopped subprocesses is also reported. WUNTRACED = WSTOPPED WRUSAGE = 1 << 20 // Record resource usage. )
Options for Wait.
const ( O_RDONLY = syscall.O_RDONLY // open the file read-only. O_WRONLY = syscall.O_WRONLY // open the file write-only. O_RDWR = syscall.O_RDWR // open the file read-write. O_APPEND = syscall.O_APPEND // open the file append-only. O_ASYNC = syscall.O_ASYNC // generate a signal when I/O is available. O_CREAT = syscall.O_CREAT // create a new file if none exists. O_EXCL = syscall.O_EXCL // used with O_CREAT, file must not exist O_NOCTTY = syscall.O_NOCTTY // do not make file the controlling tty. O_NONBLOCK = syscall.O_NONBLOCK // open in non-blocking mode. O_NDELAY = O_NONBLOCK // synonym for O_NONBLOCK O_SYNC = syscall.O_SYNC // open for synchronous I/O. O_TRUNC = syscall.O_TRUNC // if possible, truncate file when opened. O_CREATE = O_CREAT // create a new file if none exists. )
Flags to Open wrapping those of the underlying system. Not all flags may be implemented on a given system.
Variables ¶
var ( Stdin = NewFile(0, "/dev/stdin") Stdout = NewFile(1, "/dev/stdout") Stderr = NewFile(2, "/dev/stderr") )
Stdin, Stdout, and Stderr are open Files pointing to the standard input, standard output, and standard error file descriptors.
var Args []string // provided by runtime
var ENOENV = NewError("no such environment variable")
ENOENV is the Error indicating that an environment variable does not exist.
var Envs []string // provided by runtime
Functions ¶
func Environ ¶
func Environ() []string
Environ returns an array of strings representing the environment, in the form "key=value".
func Exit ¶
func Exit(code int)
Exit causes the current program to exit with the given status code. Conventionally, code zero indicates success, non-zero an error.
func Getenv ¶
Getenv retrieves the value of the environment variable named by the key. It returns the value, which will be empty if the variable is not present.
func Getpagesize ¶
func Getpagesize() int
Getpagesize returns the underlying system's memory page size.
func Lstat ¶
Lstat returns the Dir structure describing the named file and an error, if any. If the file is a symbolic link, the returned Dir describes the symbolic link. Lstat makes no attempt to follow the link.
func Open ¶
Open opens the named file with specified flag (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful, methods on the returned File can be used for I/O. It returns the File and an Error, if any.
func Pipe ¶
Pipe returns a connected pair of Files; reads from r return bytes written to w. It returns the files and an Error, if any.
func Stat ¶
Stat returns a Dir structure describing the named file and an error, if any. If name names a valid symbolic link, the returned Dir describes the file pointed at by the link and has dir.FollowedSymlink set to true. If name names an invalid symbolic link, the returned Dir describes the link itself and has dir.FollowedSymlink set to false.
Types ¶
type Dir ¶
type Dir struct { Dev uint64 // device number of file system holding file. Ino uint64 // inode number. Nlink uint64 // number of hard links. Mode uint32 // permission and mode bits. Uid uint32 // user id of owner. Gid uint32 // group id of owner. Rdev uint64 // device type for special file. Size uint64 // length in bytes. Blksize uint64 // size of blocks, in bytes. Blocks uint64 // number of blocks allocated for file. Atime_ns uint64 // access time; nanoseconds since epoch. Mtime_ns uint64 // modified time; nanoseconds since epoch. Ctime_ns uint64 // status change time; nanoseconds since epoch. Name string // name of file as presented to Open. FollowedSymlink bool // followed a symlink to get this information }
A Dir describes a file and is returned by Stat, Fstat, and Lstat
func (*Dir) IsDirectory ¶
IsDirectory reports whether the Dir describes a directory.
func (*Dir) Permission ¶
Permission returns the file permission bits.
type Errno ¶
type Errno int64
Errno is the Unix error number. Names such as EINVAL are simple wrappers to convert the error number into an Error.
type Error ¶
type Error interface {
String() string
}
An Error can represent any printable error condition.
var ( EPERM Error = Errno(syscall.EPERM) ENOENT Error = Errno(syscall.ENOENT) ESRCH Error = Errno(syscall.ESRCH) EINTR Error = Errno(syscall.EINTR) EIO Error = Errno(syscall.EIO) ENXIO Error = Errno(syscall.ENXIO) E2BIG Error = Errno(syscall.E2BIG) ENOEXEC Error = Errno(syscall.ENOEXEC) EBADF Error = Errno(syscall.EBADF) ECHILD Error = Errno(syscall.ECHILD) EDEADLK Error = Errno(syscall.EDEADLK) ENOMEM Error = Errno(syscall.ENOMEM) EACCES Error = Errno(syscall.EACCES) EFAULT Error = Errno(syscall.EFAULT) EBUSY Error = Errno(syscall.EBUSY) EEXIST Error = Errno(syscall.EEXIST) EXDEV Error = Errno(syscall.EXDEV) ENODEV Error = Errno(syscall.ENODEV) ENOTDIR Error = Errno(syscall.ENOTDIR) EISDIR Error = Errno(syscall.EISDIR) EINVAL Error = Errno(syscall.EINVAL) ENFILE Error = Errno(syscall.ENFILE) EMFILE Error = Errno(syscall.EMFILE) ENOTTY Error = Errno(syscall.ENOTTY) EFBIG Error = Errno(syscall.EFBIG) ENOSPC Error = Errno(syscall.ENOSPC) ESPIPE Error = Errno(syscall.ESPIPE) EROFS Error = Errno(syscall.EROFS) EMLINK Error = Errno(syscall.EMLINK) EPIPE Error = Errno(syscall.EPIPE) EAGAIN Error = Errno(syscall.EAGAIN) EDOM Error = Errno(syscall.EDOM) ERANGE Error = Errno(syscall.ERANGE) EADDRINUSE Error = Errno(syscall.EADDRINUSE) ECONNREFUSED Error = Errno(syscall.ECONNREFUSED) ENAMETOOLONG Error = Errno(syscall.ENAMETOOLONG) EAFNOSUPPORT Error = Errno(syscall.EAFNOSUPPORT) )
Commonly known Unix errors.
var EOF Error = eofError(0)
EOF is the Error returned by Read when no more input is available. Functions should return EOF only to signal a graceful end of input. If the EOF occurs unexpectedly in a structured data stream, the appropriate error is either io.ErrUnexpectedEOF or some other error giving more detail.
func Chmod ¶
Chmod changes the mode of the named file to mode. If the file is a symbolic link, it changes the mode of the link's target.
func Chown ¶
Chown changes the numeric uid and gid of the named file. If the file is a symbolic link, it changes the uid and gid of the link's target.
func Exec ¶
Exec replaces the current process with an execution of the program named by argv0, with arguments argv and environment envv. If successful, Exec never returns. If it fails, it returns an Error. ForkExec is almost always a better way to execute a program.
func ForkExec ¶
func ForkExec(argv0 string, argv []string, envv []string, dir string, fd []*File) (pid int, err Error)
ForkExec forks the current process and invokes Exec with the file, arguments, and environment specified by argv0, argv, and envv. It returns the process id of the forked process and an Error, if any. The fd array specifies the file descriptors to be set up in the new process: fd[0] will be Unix file descriptor 0 (standard input), fd[1] descriptor 1, and so on. A nil entry will cause the child to have no open file descriptor with that index. If dir is not empty, the child chdirs into the directory before execing the program.
func Getenverror ¶
Getenverror retrieves the value of the environment variable named by the key. It returns the value and an error, if any.
func Getwd ¶
Getwd returns a rooted path name corresponding to the current directory. If the current directory can be reached via multiple paths (due to symbolic links), Getwd may return any one of them.
func Lchown ¶
Lchown changes the numeric uid and gid of the named file. If the file is a symbolic link, it changes the uid and gid of the link itself.
func Mkdir ¶
Mkdir creates a new directory with the specified name and permission bits. It returns an error, if any.
func MkdirAll ¶
MkdirAll creates a directory named path, along with any necessary parents, and returns nil, or else returns an error. The permission bits perm are used for all directories that MkdirAll creates. If path is already a directory, MkdirAll does nothing and returns nil.
func NewSyscallError ¶
NewSyscallError returns, as an Error, a new SyscallError with the given system call name and error number. As a convenience, if errno is 0, NewSyscallError returns nil.
func Readlink ¶
Readlink reads the contents of a symbolic link: the destination of the link. It returns the contents and an Error, if any.
func RemoveAll ¶
RemoveAll removes path and any children it contains. It removes everything it can but returns the first error it encounters. If the path does not exist, RemoveAll returns nil (no error).
func Setenv ¶
Setenv sets the value of the environment variable named by the key. It returns an Error, if any.
type ErrorString ¶
type ErrorString string
A helper type that can be embedded or wrapped to simplify satisfying Error.
func (ErrorString) String ¶
func (e ErrorString) String() string
type File ¶
type File struct {
// contains filtered or unexported fields
}
File represents an open file descriptor.
func (*File) Chdir ¶
Chdir changes the current working directory to the file, which must be a directory.
func (*File) Close ¶
Close closes the File, rendering it unusable for I/O. It returns an Error, if any.
func (*File) Read ¶
Read reads up to len(b) bytes from the File. It returns the number of bytes read and an Error, if any. EOF is signaled by a zero count with err set to EOF.
func (*File) ReadAt ¶
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. EOF is signaled by a zero count with err set to EOF. ReadAt always returns a non-nil Error when n != len(b).
func (*File) Readdir ¶
Readdir reads the contents of the directory associated with file and returns an array of up to count Dir structures, as would be returned by Stat, in directory order. Subsequent calls on the same file will yield further Dirs. A negative count means to read until EOF. Readdir returns the array and an Error, if any.
func (*File) Seek ¶
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.
func (*File) Stat ¶
Stat returns the Dir structure describing file. It returns the Dir and an error, if any.
func (*File) Write ¶
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).
type LinkError ¶
LinkError records an error during a link or symlink or rename system call and the paths that caused it.
type SyscallError ¶
SyscallError records an error from a specific system call.
func (*SyscallError) String ¶
func (e *SyscallError) String() string