23 September 2017

Table of Contents


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_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_SYNC   int = syscall.O_SYNC   // open for synchronous I/O.
      O_TRUNC  int = syscall.O_TRUNC  // if possible, truncate file when opened.

const (
        PathSeparator     = '/' // OS-specific path separator
        PathListSeparator = ':' // OS-specific path list separator

const DevNull = "/dev/null"


var (
      ErrInvalid    = errors.New("invalid argument") // methods on File will return this error when the receiver is nil
      ErrPermission = errors.New("permission denied")
      ErrExist      = errors.New("file already exists")
      ErrNotExist   = errors.New("file does not exist")
      ErrClosed     = errors.New("file already closed")

var (
      Stdin  = NewFile(uintptr(syscall.Stdin), "/dev/stdin")
      Stdout = NewFile(uintptr(syscall.Stdout), "/dev/stdout")
      Stderr = NewFile(uintptr(syscall.Stderr), "/dev/stderr")

var Args []string


func Chdir(dir string) error

func Chmod(name string, mode FileMode) error

func Chown(name string, uid, gid int) error

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. If there is an error, it will be of type *PathError.

func Chtimes(name string, atime time.Time, mtime time.Time) error

func Clearenv()

func Environ() []string

func Executable() (string, error)

func Exit(code int)

func Expand(s string, mapping func(string) string) string

func ExpandEnv(s string) string

fmt.Println(os.ExpandEnv("$USER lives in ${HOME}."))

func Getegid() int

func Getenv(key string) string

func Geteuid() int

func Getgid() int

func Getgroups() ([]int, error)

func Getpagesize() int

func Getpid() int

func Getppid() int

func Getuid() int

func Getwd() (dir string, err error)

func Hostname() (name string, err error)

func IsExist(err error) bool

func IsNotExist(err error) bool

func IsPathSeparator(c uint8) bool

func IsPermission(err error) bool

func Lchown(name string, uid, gid int) error

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. If there is an error, it will be of type *PathError.

func Link(oldname, newname string) error

func LookupEnv(key string) (string, bool)

func Mkdir(name string, perm FileMode) error

func MkdirAll(path string, perm FileMode) error

func NewSyscallError(syscall string, err error) error

func Readlink(name string) (string, error)

func Remove(name string) error

func RemoveAll(path string) error

func Rename(oldpath, newpath string) error

func SameFile(fi1, fi2 FileInfo) bool

func Setenv(key, value string) error

func Symlink(oldname, newname string) error

func TempDir() string

func Truncate(name string, size int64) error

func Unsetenv(key string) error

type File

type File struct {
      // contains filtered or unexported fields

File 结构 继承了 Reader 和 Writer

func Create(name string) (*File, error)

Create creates the named file with 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.

If there is an error, it will be of type *PathError.

func NewFile(fd uintptr, name string) *File

func Open(name string) (*File, error)

func OpenFile(name string, flag int, perm FileMode) (*File, error)

f, err := os.OpenFile("notes.txt", os.O_RDWR|os.O_CREATE, 0755)

func Pipe() (r *File, w *File, err error)

func (f *File) Chdir() error

func (f *File) Chmod(mode FileMode) error

func (f *File) Chown(uid, gid int) error

func (f *File) Close() error

func (f *File) Fd() uintptr

func (f *File) Name() string

func (f *File) Read(b []byte) (n int, err error)

func (f *File) ReadAt(b []byte, off int64) (n int, err error)

func (f *File) Readdir(n int) ([]FileInfo, error)

func (f *File) Readdirnames(n int) (names []string, err error)

func (f *File) Seek(offset int64, whence int) (ret int64, err error)

func (f *File) Stat() (FileInfo, error)

func (f *File) Sync() error

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 (f *File) Truncate(size int64) error

func (f *File) Write(b []byte) (n int, err error)

func (f *File) WriteAt(b []byte, off int64) (n int, err error)

func (f *File) WriteString(s string) (n int, err error)

type FileInfo

type FileInfo interface {
      Name() string       // base name of the file
      Size() int64        // length in bytes for regular files; system-dependent for others
      Mode() FileMode     // file mode bits
      ModTime() time.Time // modification time
      IsDir() bool        // abbreviation for Mode().IsDir()
      Sys() interface{}   // underlying data source (can return nil)

func Lstat(name string) (FileInfo, error)

Lstat returns a FileInfo describing the named file.

If the file is a symbolic link, the returned FileInfo describes the symbolic link.

Lstat makes no attempt to follow the link. If there is an error, it will be of type *PathError.

func Stat(name string) (FileInfo, error)

Stat returns a FileInfo describing the named file. If there is an error, it will be of type *PathError.

type FileMode

type FileMode uint32

const (
      // The single letters are the abbreviations
      // used by the String method's formatting.
      ModeDir        FileMode = 1 << (32 - 1 - iota) // d: is a directory
      ModeAppend                                     // a: append-only
      ModeExclusive                                  // l: exclusive use
      ModeTemporary                                  // T: temporary file; Plan 9 only
      ModeSymlink                                    // L: symbolic link
      ModeDevice                                     // D: device file
      ModeNamedPipe                                  // p: named pipe (FIFO)
      ModeSocket                                     // S: Unix domain socket
      ModeSetuid                                     // u: setuid
      ModeSetgid                                     // g: setgid
      ModeCharDevice                                 // c: Unix character device, when ModeDevice is set
      ModeSticky                                     // t: sticky

      // Mask for the type bits. For regular files, none will be set.
      ModeType = ModeDir | ModeSymlink | ModeNamedPipe | ModeSocket | ModeDevice

      ModePerm FileMode = 0777 // Unix permission bits

func (m FileMode) IsDir() bool

func (m FileMode) IsRegular() bool

func (m FileMode) Perm() FileMode

func (m FileMode) String() string

type LinkError

type LinkError struct {
      Op  string
      Old string
      New string
      Err error

func (e *LinkError) Error() string

type PathError

type PathError struct {
      Op   string
      Path string
      Err  error

func (e *PathError) Error() string

type ProcAttr

type Process

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

func FindProcess(pid int) (*Process, error)

The Process it returns can be used to obtain information about the underlying operating system process.

On Unix systems, FindProcess always succeeds and returns a Process for the given pid, regardless of whether the process exists.

func StartProcess(name string, argv []string, attr *ProcAttr) (*Process, error)

StartProcess starts a new process with the program, arguments and attributes specified by name, argv and attr.
StartProcess is a low-level interface. The os/exec package provides higher-level interfaces.

If there is an error, it will be of type *PathError

func (p *Process) Kill() error

Kill causes the Process to exit immediately.

func (p *Process) Release() error

Release releases any resources associated with the Process p, rendering it unusable in the future.
Release only needs to be called if Wait is not.

func (p *Process) Signal(sig Signal) error

Signal sends a signal to the Process. Sending Interrupt on Windows is not implemented.

func (p *Process) Wait() (*ProcessState, error)

Wait waits for the Process to exit, and then returns a ProcessState describing its status and an error, if any.

Wait releases any resources associated with the Process.

On most operating systems, the Process must be a child of the current process or an error will be returned.

type ProcessState

type ProcessState struct {
      // contains filtered or unexported fields

func (p *ProcessState) Exited() bool

func (p *ProcessState) Pid() int

func (p *ProcessState) String() string

func (p *ProcessState) Success() bool

func (p *ProcessState) Sys() interface{}

Sys returns system-dependent exit information about the process. Convert it to the appropriate underlying type, such as syscall.WaitStatus on Unix, to access its contents.

func (p *ProcessState) SysUsage() interface{}

SysUsage returns system-dependent resource usage information about the exited process. Convert it to the appropriate underlying type, such as *syscall.Rusage on Unix, to access its contents. (On Unix, *syscall.Rusage matches struct rusage as defined in the getrusage(2) manual page.)

func (p *ProcessState) SystemTime() time.Duration

SystemTime returns the system CPU time of the exited process and its children.

func (p *ProcessState) UserTime() time.Duration

UserTime returns the user CPU time of the exited process and its children.

type Signal

A Signal represents an operating system signal.
The usual underlying implementation is operating system-dependent: on Unix it is syscall.Signal.

type Signal interface {
      String() string
      Signal() // to distinguish from other Stringers

var (
      Interrupt Signal = syscall.SIGINT
      Kill      Signal = syscall.SIGKILL

type SyscallError

type SyscallError struct {
      Syscall string
      Err     error

func (e *SyscallError) Error() string