Documentation ¶
Overview ¶
Package os provides a platform-independent interface to operating system functionality. The design is Unix-like. The os interface is intended to be uniform across all operating systems. Features not generally available appear in the system-specific package syscall.
Index ¶
- Constants
- Variables
- func Clearenv()
- func Create(name string) (file *File, err Error)
- func Environ() []string
- func Exit(code int)
- func Expand(s string, mapping func(string) string) string
- func FindProcess(pid int) (p *Process, err Error)
- 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 IsPathSeparator(c uint8) bool
- func Lstat(name string) (fi *FileInfo, err Error)
- func Open(name string) (file *File, err Error)
- func OpenFile(name string, flag int, perm uint32) (file *File, err Error)
- func Pipe() (r *File, w *File, err Error)
- func ShellExpand(s string) string
- func StartProcess(name string, argv []string, attr *ProcAttr) (p *Process, err Error)
- func Stat(name string) (fi *FileInfo, err Error)
- func TempDir() string
- func Wait(pid int, options int) (w *Waitmsg, err Error)
- type Errno
- type Error
- func Chdir(dir string) Error
- func Chmod(name string, mode uint32) Error
- func Chown(name string, uid, gid int) Error
- func Chtimes(name string, atime_ns int64, mtime_ns int64) Error
- func Exec(name string, argv []string, envv []string) 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 uint32) Error
- func MkdirAll(path string, perm uint32) 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 File
- func (f *File) Chdir() Error
- func (f *File) Chmod(mode uint32) 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(n int) (fi []FileInfo, err Error)
- func (f *File) Readdirnames(n int) (names []string, err Error)
- func (file *File) Seek(offset int64, whence int) (ret int64, err Error)
- func (file *File) Stat() (fi *FileInfo, err Error)
- func (file *File) Sync() (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 FileInfo
- type LinkError
- type PathError
- type ProcAttr
- type Process
- type Signal
- type SyscallError
- type UnixSignal
- 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 = syscall.WUNTRACED // Usually an alias for WSTOPPED. WRUSAGE = 1 << 20 // Record resource usage. )
Options for Wait.
const ( O_RDONLY int = syscall.O_RDONLY // open the file read-only. O_WRONLY int = syscall.O_WRONLY // open the file write-only. O_RDWR int = syscall.O_RDWR // open the file read-write. O_APPEND int = syscall.O_APPEND // append data to the file when writing. O_ASYNC int = syscall.O_ASYNC // generate a signal when I/O is available. O_CREATE int = syscall.O_CREAT // create a new file if none exists. O_EXCL int = syscall.O_EXCL // used with O_CREATE, file must not exist O_NOCTTY int = syscall.O_NOCTTY // do not make file the controlling tty. O_NONBLOCK int = syscall.O_NONBLOCK // open in non-blocking mode. O_NDELAY int = O_NONBLOCK // synonym for O_NONBLOCK O_SYNC int = syscall.O_SYNC // open for synchronous I/O. O_TRUNC int = syscall.O_TRUNC // if possible, truncate file when opened. )
Flags to Open wrapping those of the underlying system. Not all flags may be implemented on a given system.
const ( SEEK_SET int = 0 // seek relative to the origin of the file SEEK_CUR int = 1 // seek relative to the current offset SEEK_END int = 2 // seek relative to the end )
Seek whence values.
const ( PathSeparator = '/' // OS-specific path separator PathListSeparator = ':' // OS-specific path list separator )
const DevNull = "/dev/null"
DevNull is the name of the operating system's “null device.” On Unix-like systems, it is "/dev/null"; on Windows, "NUL".
Variables ¶
var ( Stdin = NewFile(syscall.Stdin, "/dev/stdin") Stdout = NewFile(syscall.Stdout, "/dev/stdout") Stderr = NewFile(syscall.Stderr, "/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 Create ¶
Create creates the named file mode 0666 (before umask), truncating it if it already exists. If successful, methods on the returned File can be used for I/O; the associated file descriptor has mode O_RDWR. It returns the File and an Error, if any.
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 Expand ¶
Expand replaces ${var} or $var in the string based on the mapping function. Invocations of undefined variables are replaced with the empty string.
func FindProcess ¶
FindProcess looks for a running process by its pid. The Process it returns can be used to obtain information about the underlying operating system process.
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 IsPathSeparator ¶
IsPathSeparator returns true if c is a directory separator character.
func Lstat ¶
Lstat returns the FileInfo structure describing the named file and an error, if any. If the file is a symbolic link, the returned FileInfo describes the symbolic link. Lstat makes no attempt to follow the link.
func Open ¶
Open opens the named file for reading. If successful, methods on the returned file can be used for reading; the associated file descriptor has mode O_RDONLY. It returns the File and an Error, if any.
func OpenFile ¶
OpenFile is the generalized open call; most users will use Open or Create instead. It 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 ShellExpand ¶
ShellExpand replaces ${var} or $var in the string according to the values of the operating system's environment variables. References to undefined variables are replaced by the empty string.
func StartProcess ¶
StartProcess starts a new process with the program, arguments and attributes specified by name, argv and attr.
StartProcess is a low-level interface. The exec package provides higher-level interfaces.
func Stat ¶
Stat returns a FileInfo structure describing the named file and an error, if any. If name names a valid symbolic link, the returned FileInfo describes the file pointed at by the link and has fi.FollowedSymlink set to true. If name names an invalid symbolic link, the returned FileInfo describes the link itself and has fi.FollowedSymlink set to false.
func TempDir ¶
func TempDir() string
TempDir returns the default directory to use for temporary files.
Types ¶
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) ETIMEDOUT Error = Errno(syscall.ETIMEDOUT) ENOTCONN Error = Errno(syscall.ENOTCONN) )
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 Chtimes ¶
Chtimes changes the access and modification times of the named file, similar to the Unix utime() or utimes() functions.
The argument times are in nanoseconds, although the underlying filesystem may truncate or round the values to a more coarse time unit.
func Exec ¶
Exec replaces the current process with an execution of the named binary, with arguments argv and environment envv. If successful, Exec never returns. If it fails, it returns an Error.
To run a child process, see StartProcess (for a low-level interface) or the exec package (for higher-level interfaces).
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 details. 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 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 n FileInfo structures, 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 os.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 os.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.
func (*File) Readdirnames ¶
Readdirnames reads and returns a slice of names from the directory f.
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 os.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 os.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.
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 FileInfo structure describing file. It returns the FileInfo and an error, if any.
func (*File) Sync ¶
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.
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 FileInfo ¶
type FileInfo 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 int // user id of owner. Gid int // group id of owner. Rdev uint64 // device type for special file. Size int64 // length in bytes. Blksize int64 // size of blocks, in bytes. Blocks int64 // number of blocks allocated for file. Atime_ns int64 // access time; nanoseconds since epoch. Mtime_ns int64 // modified time; nanoseconds since epoch. Ctime_ns int64 // status change time; nanoseconds since epoch. Name string // base name of the file name provided in Open, Stat, etc. FollowedSymlink bool // followed a symlink to get this information }
A FileInfo describes a file and is returned by Stat, Fstat, and Lstat
func (*FileInfo) IsDirectory ¶
IsDirectory reports whether the FileInfo describes a directory.
func (*FileInfo) Permission ¶
Permission returns the file permission bits.
type LinkError ¶
LinkError records an error during a link or symlink or rename system call and the paths that caused it.
type ProcAttr ¶
type ProcAttr struct { // If Dir is non-empty, the child changes into the directory before // creating the process. Dir string // If Env is non-nil, it gives the environment variables for the // new process in the form returned by Environ. // If it is nil, the result of Environ will be used. Env []string // Files specifies the open files inherited by the new process. The // first three entries correspond to standard input, standard output, and // standard error. An implementation may support additional entries, // depending on the underlying operating system. A nil entry corresponds // to that file being closed when the process starts. Files []*File // Operating system-specific process creation attributes. // Note that setting this field means that your program // may not execute properly or even compile on some // operating systems. Sys *syscall.SysProcAttr }
ProcAttr holds the attributes that will be applied to a new process started by StartProcess.
type Process ¶
type Process struct { Pid int // contains filtered or unexported fields }
Process stores the information about a process created by StartProcess.
type Signal ¶
type Signal interface {
String() string
}
A Signal can represent any operating system signal.
type SyscallError ¶
SyscallError records an error from a specific system call.
func (*SyscallError) String ¶
func (e *SyscallError) String() string
type UnixSignal ¶
type UnixSignal int32
func (UnixSignal) String ¶
func (sig UnixSignal) String() string
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package inotify implements a wrapper for the Linux inotify system.
|
Package inotify implements a wrapper for the Linux inotify system. |
Package signal implements operating system-independent signal handling.
|
Package signal implements operating system-independent signal handling. |
Package user allows user account lookups by name or id.
|
Package user allows user account lookups by name or id. |