🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# package os `import "os"` os包提供了操作系统函数的不依赖平台的接口。设计为Unix风格的,虽然错误处理是go风格的;失败的调用会返回错误值而非错误码。通常错误值里包含更多信息。例如,如果某个使用一个文件名的调用(如Open、Stat)失败了,打印错误时会包含该文件名,错误类型将为\*PathError,其内部可以解包获得更多信息。 os包的接口规定为在所有操作系统中都是一致的。非公用的属性可以从操作系统特定的[syscall](http://godoc.org/syscall)包获取。 下面是一个简单的例子,打开一个文件并从中读取一些数据: ``` file, err := os.Open("file.go") // For read access. if err != nil { log.Fatal(err) } ``` 如果打开失败,错误字符串是自解释的,例如: ``` open file.go: no such file or directory ``` 文件的信息可以读取进一个[]byte切片。Read和Write方法从切片参数获取其内的字节数。 ``` data := make([]byte, 100) count, err := file.Read(data) if err != nil { log.Fatal(err) } fmt.Printf("read %d bytes: %q\n", count, data[:count]) ``` ## Index * [Constants](#pkg-constants) * [Variables](#pkg-variables) * [func Hostname() (name string, err error)](#Hostname) * [func Getpagesize() int](#Getpagesize) * [func Environ() []string](#Environ) * [func Getenv(key string) string](#Getenv) * [func Setenv(key, value string) error](#Setenv) * [func Clearenv()](#Clearenv) * [func Exit(code int)](#Exit) * [func Expand(s string, mapping func(string) string) string](#Expand) * [func ExpandEnv(s string) string](#ExpandEnv) * [func Getuid() int](#Getuid) * [func Geteuid() int](#Geteuid) * [func Getgid() int](#Getgid) * [func Getegid() int](#Getegid) * [func Getgroups() ([]int, error)](#Getgroups) * [func Getpid() int](#Getpid) * [func Getppid() int](#Getppid) * [type Signal](#Signal) * [type PathError](#PathError) * [func (e \*PathError) Error() string](#PathError.Error) * [type LinkError](#LinkError) * [func (e \*LinkError) Error() string](#LinkError.Error) * [type SyscallError](#SyscallError) * [func (e \*SyscallError) Error() string](#SyscallError.Error) * [func NewSyscallError(syscall string, err error) error](#NewSyscallError) * [type FileMode](#FileMode) * [func (m FileMode) IsDir() bool](#FileMode.IsDir) * [func (m FileMode) IsRegular() bool](#FileMode.IsRegular) * [func (m FileMode) Perm() FileMode](#FileMode.Perm) * [func (m FileMode) String() string](#FileMode.String) * [type FileInfo](#FileInfo) * [func Stat(name string) (fi FileInfo, err error)](#Stat) * [func Lstat(name string) (fi FileInfo, err error)](#Lstat) * [func IsPathSeparator(c uint8) bool](#IsPathSeparator) * [func IsExist(err error) bool](#IsExist) * [func IsNotExist(err error) bool](#IsNotExist) * [func IsPermission(err error) bool](#IsPermission) * [func Getwd() (dir string, err error)](#Getwd) * [func Chdir(dir string) error](#Chdir) * [func Chmod(name string, mode FileMode) error](#Chmod) * [func Chown(name string, uid, gid int) error](#Chown) * [func Lchown(name string, uid, gid int) error](#Lchown) * [func Chtimes(name string, atime time.Time, mtime time.Time) error](#Chtimes) * [func Mkdir(name string, perm FileMode) error](#Mkdir) * [func MkdirAll(path string, perm FileMode) error](#MkdirAll) * [func Rename(oldpath, newpath string) error](#Rename) * [func Truncate(name string, size int64) error](#Truncate) * [func Remove(name string) error](#Remove) * [func RemoveAll(path string) error](#RemoveAll) * [func Readlink(name string) (string, error)](#Readlink) * [func Symlink(oldname, newname string) error](#Symlink) * [func Link(oldname, newname string) error](#Link) * [func SameFile(fi1, fi2 FileInfo) bool](#SameFile) * [func TempDir() string](#TempDir) * [type File](#File) * [func Create(name string) (file \*File, err error)](#Create) * [func Open(name string) (file \*File, err error)](#Open) * [func OpenFile(name string, flag int, perm FileMode) (file \*File, err error)](#OpenFile) * [func NewFile(fd uintptr, name string) \*File](#NewFile) * [func Pipe() (r \*File, w \*File, err error)](#Pipe) * [func (f \*File) Name() string](#File.Name) * [func (f \*File) Stat() (fi FileInfo, err error)](#File.Stat) * [func (f \*File) Fd() uintptr](#File.Fd) * [func (f \*File) Chdir() error](#File.Chdir) * [func (f \*File) Chmod(mode FileMode) error](#File.Chmod) * [func (f \*File) Chown(uid, gid int) error](#File.Chown) * [func (f \*File) Readdir(n int) (fi []FileInfo, err error)](#File.Readdir) * [func (f \*File) Readdirnames(n int) (names []string, err error)](#File.Readdirnames) * [func (f \*File) Truncate(size int64) error](#File.Truncate) * [func (f \*File) Read(b []byte) (n int, err error)](#File.Read) * [func (f \*File) ReadAt(b []byte, off int64) (n int, err error)](#File.ReadAt) * [func (f \*File) Write(b []byte) (n int, err error)](#File.Write) * [func (f \*File) WriteString(s string) (ret int, err error)](#File.WriteString) * [func (f \*File) WriteAt(b []byte, off int64) (n int, err error)](#File.WriteAt) * [func (f \*File) Seek(offset int64, whence int) (ret int64, err error)](#File.Seek) * [func (f \*File) Sync() (err error)](#File.Sync) * [func (f \*File) Close() error](#File.Close) * [type ProcAttr](#ProcAttr) * [type Process](#Process) * [func FindProcess(pid int) (p \*Process, err error)](#FindProcess) * [func StartProcess(name string, argv []string, attr \*ProcAttr) (\*Process, error)](#StartProcess) * [func (p \*Process) Signal(sig Signal) error](#Process.Signal) * [func (p \*Process) Kill() error](#Process.Kill) * [func (p \*Process) Wait() (\*ProcessState, error)](#Process.Wait) * [func (p \*Process) Release() error](#Process.Release) * [type ProcessState](#ProcessState) * [func (p \*ProcessState) Pid() int](#ProcessState.Pid) * [func (p \*ProcessState) Exited() bool](#ProcessState.Exited) * [func (p \*ProcessState) Success() bool](#ProcessState.Success) * [func (p \*ProcessState) SystemTime() time.Duration](#ProcessState.SystemTime) * [func (p \*ProcessState) UserTime() time.Duration](#ProcessState.UserTime) * [func (p \*ProcessState) Sys() interface{}](#ProcessState.Sys) * [func (p \*ProcessState) SysUsage() interface{}](#ProcessState.SysUsage) * [func (p \*ProcessState) String() string](#ProcessState.String) ## Constants ``` const ( O_RDONLY int = syscall.O_RDONLY // 只读模式打开文件 O_WRONLY int = syscall.O_WRONLY // 只写模式打开文件 O_RDWR int = syscall.O_RDWR // 读写模式打开文件 O_APPEND int = syscall.O_APPEND // 写操作时将数据附加到文件尾部 O_CREATE int = syscall.O_CREAT // 如果不存在将创建一个新文件 O_EXCL int = syscall.O_EXCL // 和O_CREATE配合使用,文件必须不存在 O_SYNC int = syscall.O_SYNC // 打开文件用于同步I/O O_TRUNC int = syscall.O_TRUNC // 如果可能,打开时清空文件 ) ``` 用于包装底层系统的参数用于Open函数,不是所有的flag都能在特定系统里使用的。 ``` const ( SEEK_SET int = 0 // 相对于文件起始位置seek SEEK_CUR int = 1 // 相对于文件当前位置seek SEEK_END int = 2 // 相对于文件结尾位置seek ) ``` 指定Seek函数从何处开始搜索(即相对位置) ``` const ( PathSeparator = '/' // 操作系统指定的路径分隔符 PathListSeparator = ':' // 操作系统指定的表分隔符 ) ``` ``` const DevNull = "/dev/null" ``` DevNull是操作系统空设备的名字。在类似Unix的操作系统中,是"/dev/null";在Windows中,为"NUL"。 ## Variables ``` var ( ErrInvalid = errors.New("invalid argument") ErrPermission = errors.New("permission denied") ErrExist = errors.New("file already exists") ErrNotExist = errors.New("file does not exist") ) ``` 一些可移植的、共有的系统调用错误。 ``` var ( Stdin = NewFile(uintptr(syscall.Stdin), "/dev/stdin") Stdout = NewFile(uintptr(syscall.Stdout), "/dev/stdout") Stderr = NewFile(uintptr(syscall.Stderr), "/dev/stderr") ) ``` Stdin、Stdout和Stderr是指向标准输入、标准输出、标准错误输出的文件描述符。 ``` var Args []string ``` Args保管了命令行参数,第一个是程序名。 ## func [Hostname](https://github.com/golang/go/blob/master/src/os/doc.go#L92 "View Source") ``` func Hostname() (name string, err error) ``` Hostname返回内核提供的主机名。 ## func [Getpagesize](https://github.com/golang/go/blob/master/src/os/types.go#L13 "View Source") ``` func Getpagesize() int ``` Getpagesize返回底层的系统内存页的尺寸。 ## func [Environ](https://github.com/golang/go/blob/master/src/os/env.go#L101 "View Source") ``` func Environ() []string ``` Environ返回表示环境变量的格式为"key=value"的字符串的切片拷贝。 ## func [Getenv](https://github.com/golang/go/blob/master/src/os/env.go#L79 "View Source") ``` func Getenv(key string) string ``` Getenv检索并返回名为key的环境变量的值。如果不存在该环境变量会返回空字符串。 ## func [Setenv](https://github.com/golang/go/blob/master/src/os/env.go#L86 "View Source") ``` func Setenv(key, value string) error ``` Setenv设置名为key的环境变量。如果出错会返回该错误。 ## func [Clearenv](https://github.com/golang/go/blob/master/src/os/env.go#L95 "View Source") ``` func Clearenv() ``` Clearenv删除所有环境变量。 ## func [Exit](https://github.com/golang/go/blob/master/src/os/proc.go#L36 "View Source") ``` func Exit(code int) ``` Exit让当前程序以给出的状态码code退出。一般来说,状态码0表示成功,非0表示出错。程序会立刻终止,defer的函数不会被执行。 ## func [Expand](https://github.com/golang/go/blob/master/src/os/env.go#L13 "View Source") ``` func Expand(s string, mapping func(string) string) string ``` Expand函数替换s中的${var}或$var为mapping(var)。例如,os.ExpandEnv(s)等价于os.Expand(s, os.Getenv)。 ## func [ExpandEnv](https://github.com/golang/go/blob/master/src/os/env.go#L32 "View Source") ``` func ExpandEnv(s string) string ``` ExpandEnv函数替换s中的${var}或$var为名为var 的环境变量的值。引用未定义环境变量会被替换为空字符串。 ## func [Getuid](https://github.com/golang/go/blob/master/src/os/proc.go#L15 "View Source") ``` func Getuid() int ``` Getuid返回调用者的用户ID。 ## func [Geteuid](https://github.com/golang/go/blob/master/src/os/proc.go#L18 "View Source") ``` func Geteuid() int ``` Geteuid返回调用者的有效用户ID。 ## func [Getgid](https://github.com/golang/go/blob/master/src/os/proc.go#L21 "View Source") ``` func Getgid() int ``` Getgid返回调用者的组ID。 ## func [Getegid](https://github.com/golang/go/blob/master/src/os/proc.go#L24 "View Source") ``` func Getegid() int ``` Getegid返回调用者的有效组ID。 ## func [Getgroups](https://github.com/golang/go/blob/master/src/os/proc.go#L27 "View Source") ``` func Getgroups() ([]int, error) ``` Getgroups返回调用者所属的所有用户组的组ID。 ## func [Getpid](https://github.com/golang/go/blob/master/src/os/exec.go#L67 "View Source") ``` func Getpid() int ``` Getpid返回调用者所在进程的进程ID。 ## func [Getppid](https://github.com/golang/go/blob/master/src/os/exec.go#L70 "View Source") ``` func Getppid() int ``` Getppid返回调用者所在进程的父进程的进程ID。 ## type [Signal](https://github.com/golang/go/blob/master/src/os/exec.go#L61 "View Source") ``` type Signal interface { String() string Signal() // 用来区分其他实现了Stringer接口的类型 } ``` Signal代表一个操作系统信号。一般其底层实现是依赖于操作系统的:在Unix中,它是syscall.Signal类型。 ``` var ( Interrupt Signal = syscall.SIGINT Kill Signal = syscall.SIGKILL ) ``` 仅有的肯定会被所有操作系统提供的信号,Interrupt(中断信号)和Kill(强制退出信号)。 ## type [PathError](https://github.com/golang/go/blob/master/src/os/error.go#L20 "View Source") ``` type PathError struct { Op string Path string Err error } ``` PathError记录一个错误,以及导致错误的路径。 ### func (\*PathError) [Error](https://github.com/golang/go/blob/master/src/os/error.go#L26 "View Source") ``` func (e *PathError) Error() string ``` ## type [LinkError](https://github.com/golang/go/blob/master/src/os/file.go#L77 "View Source") ``` type LinkError struct { Op string Old string New string Err error } ``` LinkError记录在Link、Symlink、Rename系统调用时出现的错误,以及导致错误的路径。 ### func (\*LinkError) [Error](https://github.com/golang/go/blob/master/src/os/file.go#L84 "View Source") ``` func (e *LinkError) Error() string ``` ## type [SyscallError](https://github.com/golang/go/blob/master/src/os/error.go#L29 "View Source") ``` type SyscallError struct { Syscall string Err error } ``` SyscallError记录某个系统调用出现的错误。 ### func (\*SyscallError) [Error](https://github.com/golang/go/blob/master/src/os/error.go#L34 "View Source") ``` func (e *SyscallError) Error() string ``` ### func [NewSyscallError](https://github.com/golang/go/blob/master/src/os/error.go#L39 "View Source") ``` func NewSyscallError(syscall string, err error) error ``` NewSyscallError返回一个指定系统调用名称和错误细节的SyscallError。如果err为nil,本函数会返回nil。 ## type [FileMode](https://github.com/golang/go/blob/master/src/os/types.go#L30 "View Source") ``` type FileMode uint32 ``` FileMode代表文件的模式和权限位。这些字位在所有的操作系统都有相同的含义,因此文件的信息可以在不同的操作系统之间安全的移植。不是所有的位都能用于所有的系统,唯一共有的是用于表示目录的ModeDir位。 ``` const ( // 单字符是被String方法用于格式化的属性缩写。 ModeDir FileMode = 1 << (32 - 1 - iota) // d: 目录 ModeAppend // a: 只能写入,且只能写入到末尾 ModeExclusive // l: 用于执行 ModeTemporary // T: 临时文件(非备份文件) ModeSymlink // L: 符号链接(不是快捷方式文件) ModeDevice // D: 设备 ModeNamedPipe // p: 命名管道(FIFO) ModeSocket // S: Unix域socket ModeSetuid // u: 表示文件具有其创建者用户id权限 ModeSetgid // g: 表示文件具有其创建者组id的权限 ModeCharDevice // c: 字符设备,需已设置ModeDevice ModeSticky // t: 只有root/创建者能删除/移动文件 // 覆盖所有类型位(用于通过&获取类型位),对普通文件,所有这些位都不应被设置 ModeType = ModeDir | ModeSymlink | ModeNamedPipe | ModeSocket | ModeDevice ModePerm FileMode = 0777 // 覆盖所有Unix权限位(用于通过&获取类型位) ) ``` 这些被定义的位是FileMode最重要的位。另外9个不重要的位为标准Unix rwxrwxrwx权限(任何人都可读、写、运行)。这些(重要)位的值应被视为公共API的一部分,可能会用于线路协议或硬盘标识:它们不能被修改,但可以添加新的位。 ### func (FileMode) [IsDir](https://github.com/golang/go/blob/master/src/os/types.go#L87 "View Source") ``` func (m FileMode) IsDir() bool ``` IsDir报告m是否是一个目录。 ### func (FileMode) [IsRegular](https://github.com/golang/go/blob/master/src/os/types.go#L93 "View Source") ``` func (m FileMode) IsRegular() bool ``` IsRegular报告m是否是一个普通文件。 ### func (FileMode) [Perm](https://github.com/golang/go/blob/master/src/os/types.go#L98 "View Source") ``` func (m FileMode) Perm() FileMode ``` Perm方法返回m的Unix权限位。 ### func (FileMode) [String](https://github.com/golang/go/blob/master/src/os/types.go#L59 "View Source") ``` func (m FileMode) String() string ``` ## type [FileInfo](https://github.com/golang/go/blob/master/src/os/types.go#L16 "View Source") ``` type FileInfo interface { Name() string // 文件的名字(不含扩展名) Size() int64 // 普通文件返回值表示其大小;其他文件的返回值含义各系统不同 Mode() FileMode // 文件的模式位 ModTime() time.Time // 文件的修改时间 IsDir() bool // 等价于Mode().IsDir() Sys() interface{} // 底层数据来源(可以返回nil) } ``` FileInfo用来描述一个文件对象。 ### func [Stat](https://github.com/golang/go/blob/master/src/os/file_unix.go#L131 "View Source") ``` func Stat(name string) (fi FileInfo, err error) ``` Stat返回一个描述name指定的文件对象的FileInfo。如果指定的文件对象是一个符号链接,返回的FileInfo描述该符号链接指向的文件的信息,本函数会尝试跳转该链接。如果出错,返回的错误值为\*PathError类型。 ### func [Lstat](https://github.com/golang/go/blob/master/src/os/file_unix.go#L144 "View Source") ``` func Lstat(name string) (fi FileInfo, err error) ``` Lstat返回一个描述name指定的文件对象的FileInfo。如果指定的文件对象是一个符号链接,返回的FileInfo描述该符号链接的信息,本函数不会试图跳转该链接。如果出错,返回的错误值为\*PathError类型。 ## func [IsPathSeparator](https://github.com/golang/go/blob/master/src/os/path_unix.go#L15 "View Source") ``` func IsPathSeparator(c uint8) bool ``` IsPathSeparator返回字符c是否是一个路径分隔符。 ## func [IsExist](https://github.com/golang/go/blob/master/src/os/error.go#L49 "View Source") ``` func IsExist(err error) bool ``` 返回一个布尔值说明该错误是否表示一个文件或目录已经存在。ErrExist和一些系统调用错误会使它返回真。 ## func [IsNotExist](https://github.com/golang/go/blob/master/src/os/error.go#L56 "View Source") ``` func IsNotExist(err error) bool ``` 返回一个布尔值说明该错误是否表示一个文件或目录不存在。ErrNotExist和一些系统调用错误会使它返回真。 ## func [IsPermission](https://github.com/golang/go/blob/master/src/os/error.go#L63 "View Source") ``` func IsPermission(err error) bool ``` 返回一个布尔值说明该错误是否表示因权限不足要求被拒绝。ErrPermission和一些系统调用错误会使它返回真。 ## func [Getwd](https://github.com/golang/go/blob/master/src/os/getwd.go#L25 "View Source") ``` func Getwd() (dir string, err error) ``` Getwd返回一个对应当前工作目录的根路径。如果当前目录可以经过多条路径抵达(因为硬链接),Getwd会返回其中一个。 ## func [Chdir](https://github.com/golang/go/blob/master/src/os/file.go#L214 "View Source") ``` func Chdir(dir string) error ``` Chdir将当前工作目录修改为dir指定的目录。如果出错,会返回\*PathError底层类型的错误。 ## func [Chmod](https://github.com/golang/go/blob/master/src/os/file_posix.go#L78 "View Source") ``` func Chmod(name string, mode FileMode) error ``` Chmod修改name指定的文件对象的mode。如果name指定的文件是一个符号链接,它会修改该链接的目的地文件的mode。如果出错,会返回\*PathError底层类型的错误。 ## func [Chown](https://github.com/golang/go/blob/master/src/os/file_posix.go#L100 "View Source") ``` func Chown(name string, uid, gid int) error ``` Chmod修改name指定的文件对象的用户id和组id。如果name指定的文件是一个符号链接,它会修改该链接的目的地文件的用户id和组id。如果出错,会返回\*PathError底层类型的错误。 ## func [Lchown](https://github.com/golang/go/blob/master/src/os/file_posix.go#L110 "View Source") ``` func Lchown(name string, uid, gid int) error ``` Chmod修改name指定的文件对象的用户id和组id。如果name指定的文件是一个符号链接,它会修改该符号链接自身的用户id和组id。如果出错,会返回\*PathError底层类型的错误。 ## func [Chtimes](https://github.com/golang/go/blob/master/src/os/file_posix.go#L161 "View Source") ``` func Chtimes(name string, atime time.Time, mtime time.Time) error ``` Chtimes修改name指定的文件对象的访问时间和修改时间,类似Unix的utime()或utimes()函数。底层的文件系统可能会截断/舍入时间单位到更低的精确度。如果出错,会返回\*PathError底层类型的错误。 ## func [Mkdir](https://github.com/golang/go/blob/master/src/os/file.go#L204 "View Source") ``` func Mkdir(name string, perm FileMode) error ``` Mkdir使用指定的权限和名称创建一个目录。如果出错,会返回\*PathError底层类型的错误。 ## func [MkdirAll](https://github.com/golang/go/blob/master/src/os/path.go#L19 "View Source") ``` func MkdirAll(path string, perm FileMode) error ``` MkdirAll使用指定的权限和名称创建一个目录,包括任何必要的上级目录,并返回nil,否则返回错误。权限位perm会应用在每一个被本函数创建的目录上。如果path指定了一个已经存在的目录,MkdirAll不做任何操作并返回nil。 ## func [Rename](https://github.com/golang/go/blob/master/src/os/file.go#L255 "View Source") ``` func Rename(oldpath, newpath string) error ``` Rename修改一个文件的名字,移动一个文件。可能会有一些个操作系统特定的限制。 ## func [Truncate](https://github.com/golang/go/blob/master/src/os/file_unix.go#L251 "View Source") ``` func Truncate(name string, size int64) error ``` Truncate修改name指定的文件的大小。如果该文件为一个符号链接,将修改链接指向的文件的大小。如果出错,会返回\*PathError底层类型的错误。 ## func [Remove](https://github.com/golang/go/blob/master/src/os/file_unix.go#L260 "View Source") ``` func Remove(name string) error ``` Remove删除name指定的文件或目录。如果出错,会返回\*PathError底层类型的错误。 ## func [RemoveAll](https://github.com/golang/go/blob/master/src/os/path.go#L66 "View Source") ``` func RemoveAll(path string) error ``` RemoveAll删除path指定的文件,或目录及它包含的任何下级对象。它会尝试删除所有东西,除非遇到错误并返回。如果path指定的对象不存在,RemoveAll会返回nil而不返回错误。 ## func [Readlink](https://github.com/golang/go/blob/master/src/os/file_posix.go#L38 "View Source") ``` func Readlink(name string) (string, error) ``` Readlink获取name指定的符号链接文件指向的文件的路径。如果出错,会返回\*PathError底层类型的错误。 ## func [Symlink](https://github.com/golang/go/blob/master/src/os/file_posix.go#L28 "View Source") ``` func Symlink(oldname, newname string) error ``` Symlink创建一个名为newname指向oldname的符号链接。如果出错,会返回* LinkError底层类型的错误。 ## func [Link](https://github.com/golang/go/blob/master/src/os/file_posix.go#L18 "View Source") ``` func Link(oldname, newname string) error ``` Link创建一个名为newname指向oldname的硬链接。如果出错,会返回* LinkError底层类型的错误。 ## func [SameFile](https://github.com/golang/go/blob/master/src/os/types.go#L111 "View Source") ``` func SameFile(fi1, fi2 FileInfo) bool ``` SameFile返回fi1和fi2是否在描述同一个文件。例如,在Unix这表示二者底层结构的设备和索引节点是相同的;在其他系统中可能是根据路径名确定的。SameFile应只使用本包Stat函数返回的FileInfo类型值为参数,其他情况下,它会返回假。 ## func [TempDir](https://github.com/golang/go/blob/master/src/os/file_unix.go#L308 "View Source") ``` func TempDir() string ``` TempDir返回一个用于保管临时文件的默认目录。 ## type [File](https://github.com/golang/go/blob/master/src/os/file_unix.go#L16 "View Source") ``` type File struct { // 内含隐藏或非导出字段 } ``` File代表一个打开的文件对象。 ### func [Create](https://github.com/golang/go/blob/master/src/os/file.go#L247 "View Source") ``` func Create(name string) (file *File, err error) ``` Create采用模式0666(任何人都可读写,不可执行)创建一个名为name的文件,如果文件已存在会截断它(为空文件)。如果成功,返回的文件对象可用于I/O;对应的文件描述符具有O_RDWR模式。如果出错,错误底层类型是\*PathError。 ### func [Open](https://github.com/golang/go/blob/master/src/os/file.go#L238 "View Source") ``` func Open(name string) (file *File, err error) ``` Open打开一个文件用于读取。如果操作成功,返回的文件对象的方法可用于读取数据;对应的文件描述符具有O_RDONLY模式。如果出错,错误底层类型是\*PathError。 ### func [OpenFile](https://github.com/golang/go/blob/master/src/os/file_unix.go#L76 "View Source") ``` func OpenFile(name string, flag int, perm FileMode) (file *File, err error) ``` OpenFile是一个更一般性的文件打开函数,大多数调用者都应用Open或Create代替本函数。它会使用指定的选项(如O_RDONLY等)、指定的模式(如0666等)打开指定名称的文件。如果操作成功,返回的文件对象可用于I/O。如果出错,错误底层类型是\*PathError。 ### func [NewFile](https://github.com/golang/go/blob/master/src/os/file_unix.go#L40 "View Source") ``` func NewFile(fd uintptr, name string) *File ``` NewFile使用给出的Unix文件描述符和名称创建一个文件。 ### func [Pipe](https://github.com/golang/go/blob/master/src/os/pipe_linux.go#L11 "View Source") ``` func Pipe() (r *File, w *File, err error) ``` Pipe返回一对关联的文件对象。从r的读取将返回写入w的数据。本函数会返回两个文件对象和可能的错误。 ### func (\*File) [Name](https://github.com/golang/go/blob/master/src/os/file.go#L45 "View Source") ``` func (f *File) Name() string ``` Name方法返回(提供给Open/Create等方法的)文件名称。 ### func (\*File) [Stat](https://github.com/golang/go/blob/master/src/os/file_unix.go#L117 "View Source") ``` func (f *File) Stat() (fi FileInfo, err error) ``` Stat返回描述文件f的FileInfo类型值。如果出错,错误底层类型是\*PathError。 ### func (\*File) [Fd](https://github.com/golang/go/blob/master/src/os/file_unix.go#L32 "View Source") ``` func (f *File) Fd() uintptr ``` Fd返回与文件f对应的整数类型的Unix文件描述符。 ### func (\*File) [Chdir](https://github.com/golang/go/blob/master/src/os/file.go#L224 "View Source") ``` func (f *File) Chdir() error ``` Chdir将当前工作目录修改为f,f必须是一个目录。如果出错,错误底层类型是\*PathError。 ### func (\*File) [Chmod](https://github.com/golang/go/blob/master/src/os/file_posix.go#L87 "View Source") ``` func (f *File) Chmod(mode FileMode) error ``` Chmod修改文件的模式。如果出错,错误底层类型是\*PathError。 ### func (\*File) [Chown](https://github.com/golang/go/blob/master/src/os/file_posix.go#L119 "View Source") ``` func (f *File) Chown(uid, gid int) error ``` Chown修改文件的用户ID和组ID。如果出错,错误底层类型是\*PathError。 ### func (\*File) [Readdir](https://github.com/golang/go/blob/master/src/os/doc.go#L111 "View Source") ``` func (f *File) Readdir(n int) (fi []FileInfo, err error) ``` Readdir读取目录f的内容,返回一个有n个成员的[]FileInfo,这些FileInfo是被Lstat返回的,采用目录顺序。对本函数的下一次调用会返回上一次调用剩余未读取的内容的信息。 如果n&gt;0,Readdir函数会返回一个最多n个成员的切片。这时,如果Readdir返回一个空切片,它会返回一个非nil的错误说明原因。如果到达了目录f的结尾,返回值err会是io.EOF。 如果n&lt;=0,Readdir函数返回目录中剩余所有文件对象的FileInfo构成的切片。此时,如果Readdir调用成功(读取所有内容直到结尾),它会返回该切片和nil的错误值。如果在到达结尾前遇到错误,会返回之前成功读取的FileInfo构成的切片和该错误。 ### func (\*File) [Readdirnames](https://github.com/golang/go/blob/master/src/os/doc.go#L130 "View Source") ``` func (f *File) Readdirnames(n int) (names []string, err error) ``` Readdir读取目录f的内容,返回一个有n个成员的[]string,切片成员为目录中文件对象的名字,采用目录顺序。对本函数的下一次调用会返回上一次调用剩余未读取的内容的信息。 如果n&gt;0,Readdir函数会返回一个最多n个成员的切片。这时,如果Readdir返回一个空切片,它会返回一个非nil的错误说明原因。如果到达了目录f的结尾,返回值err会是io.EOF。 如果n&lt;=0,Readdir函数返回目录中剩余所有文件对象的名字构成的切片。此时,如果Readdir调用成功(读取所有内容直到结尾),它会返回该切片和nil的错误值。如果在到达结尾前遇到错误,会返回之前成功读取的名字构成的切片和该错误。 ### func (\*File) [Truncate](https://github.com/golang/go/blob/master/src/os/file_posix.go#L132 "View Source") ``` func (f *File) Truncate(size int64) error ``` Truncate改变文件的大小,它不会改变I/O的当前位置。 如果截断文件,多出的部分就会被丢弃。如果出错,错误底层类型是\*PathError。 ### func (\*File) [Read](https://github.com/golang/go/blob/master/src/os/file.go#L91 "View Source") ``` func (f *File) Read(b []byte) (n int, err error) ``` Read方法从f中读取最多len(b)字节数据并写入b。它返回读取的字节数和可能遇到的任何错误。文件终止标志是读取0个字节且返回值err为io.EOF。 ### func (\*File) [ReadAt](https://github.com/golang/go/blob/master/src/os/file.go#L112 "View Source") ``` func (f *File) ReadAt(b []byte, off int64) (n int, err error) ``` ReadAt从指定的位置(相对于文件开始位置)读取len(b)字节数据并写入b。它返回读取的字节数和可能遇到的任何错误。当n&lt;len(b)时,本方法总是会返回错误;如果是因为到达文件结尾,返回值err会是io.EOF。 ### func (\*File) [Write](https://github.com/golang/go/blob/master/src/os/file.go#L135 "View Source") ``` func (f *File) Write(b []byte) (n int, err error) ``` Write向文件中写入len(b)字节数据。它返回写入的字节数和可能遇到的任何错误。如果返回值n!=len(b),本方法会返回一个非nil的错误。 ### func (\*File) [WriteString](https://github.com/golang/go/blob/master/src/os/file.go#L195 "View Source") ``` func (f *File) WriteString(s string) (ret int, err error) ``` WriteString类似Write,但接受一个字符串参数。 ### func (\*File) [WriteAt](https://github.com/golang/go/blob/master/src/os/file.go#L158 "View Source") ``` func (f *File) WriteAt(b []byte, off int64) (n int, err error) ``` WriteAt在指定的位置(相对于文件开始位置)写入len(b)字节数据。它返回写入的字节数和可能遇到的任何错误。如果返回值n!=len(b),本方法会返回一个非nil的错误。 ### func (\*File) [Seek](https://github.com/golang/go/blob/master/src/os/file.go#L179 "View Source") ``` func (f *File) Seek(offset int64, whence int) (ret int64, err error) ``` Seek设置下一次读/写的位置。offset为相对偏移量,而whence决定相对位置:0为相对文件开头,1为相对当前位置,2为相对文件结尾。它返回新的偏移量(相对开头)和可能的错误。 ### func (\*File) [Sync](https://github.com/golang/go/blob/master/src/os/file_posix.go#L145 "View Source") ``` func (f *File) Sync() (err error) ``` Sync递交文件的当前内容进行稳定的存储。一般来说,这表示将文件系统的最近写入的数据在内存中的拷贝刷新到硬盘中稳定保存。 ### func (\*File) [Close](https://github.com/golang/go/blob/master/src/os/file_unix.go#L93 "View Source") ``` func (f *File) Close() error ``` Close关闭文件f,使文件不能用于读写。它返回可能出现的错误。 ## type [ProcAttr](https://github.com/golang/go/blob/master/src/os/exec.go#L36 "View Source") ``` type ProcAttr struct { // 如果Dir非空,子进程会在创建进程前先进入该目录。(即设为当前工作目录) Dir string // 如果Env非空,它会作为新进程的环境变量。必须采用Environ返回值的格式。 // 如果Env为空字符串,将使用Environ函数的返回值。 Env []string // Files指定被新进程继承的活动文件对象。 // 前三个绑定为标准输入、标准输出、标准错误输出。 // 依赖底层操作系统的实现可能会支持额外的数据出入途径。 // nil条目相当于在进程开始时关闭的文件对象。 Files []*File // 操作系统特定的创建属性。 // 注意设置本字段意味着你的程序可能会运作失常甚至在某些操作系统中无法通过编译。 Sys *syscall.SysProcAttr } ``` ProcAttr保管将被StartProcess函数用于一个新进程的属性。 ## type [Process](https://github.com/golang/go/blob/master/src/os/exec.go#L14 "View Source") ``` type Process struct { Pid int // 内含隐藏或非导出字段 } ``` Process保管一个被StarProcess创建的进程的信息。 ### func [FindProcess](https://github.com/golang/go/blob/master/src/os/doc.go#L12 "View Source") ``` func FindProcess(pid int) (p *Process, err error) ``` FindProcess根据进程id查找一个运行中的进程。函数返回的进程对象可以用于获取其关于底层操作系统进程的信息。 ### func [StartProcess](https://github.com/golang/go/blob/master/src/os/doc.go#L23 "View Source") ``` func StartProcess(name string, argv []string, attr *ProcAttr) (*Process, error) ``` StartProcess使用提供的属性、程序名、命令行参数开始一个新进程。StartProcess函数是一个低水平的接口。os/exec包提供了高水平的接口,应该尽量使用该包。如果出错,错误的底层类型会是\*PathError。 ### func (\*Process) [Signal](https://github.com/golang/go/blob/master/src/os/doc.go#L50 "View Source") ``` func (p *Process) Signal(sig Signal) error ``` Signal方法向进程发送一个信号。在windows中向进程发送Interrupt信号尚未实现。 ### func (\*Process) [Kill](https://github.com/golang/go/blob/master/src/os/doc.go#L35 "View Source") ``` func (p *Process) Kill() error ``` Kill让进程立刻退出。 ### func (\*Process) [Wait](https://github.com/golang/go/blob/master/src/os/doc.go#L44 "View Source") ``` func (p *Process) Wait() (*ProcessState, error) ``` Wait方法阻塞直到进程退出,然后返回一个描述ProcessState描述进程的状态和可能的错误。Wait方法会释放绑定到进程p的所有资源。在大多数操作系统中,进程p必须是当前进程的子进程,否则会返回错误。 ### func (\*Process) [Release](https://github.com/golang/go/blob/master/src/os/doc.go#L30 "View Source") ``` func (p *Process) Release() error ``` Release释放进程p绑定的所有资源, 使它们(资源)不能再被(进程p)使用。只有没有调用Wait方法时才需要调用本方法。 ## type [ProcessState](https://github.com/golang/go/blob/master/src/os/exec_posix.go#L57 "View Source") ``` type ProcessState struct { // 内含隐藏或非导出字段 } ``` ProcessState保管Wait函数报告的某个已退出进程的信息。 ### func (\*ProcessState) [Pid](https://github.com/golang/go/blob/master/src/os/exec_posix.go#L64 "View Source") ``` func (p *ProcessState) Pid() int ``` Pi返回一个已退出的进程的进程id。 ### func (\*ProcessState) [Exited](https://github.com/golang/go/blob/master/src/os/doc.go#L65 "View Source") ``` func (p *ProcessState) Exited() bool ``` Exited报告进程是否已退出。 ### func (\*ProcessState) [Success](https://github.com/golang/go/blob/master/src/os/doc.go#L71 "View Source") ``` func (p *ProcessState) Success() bool ``` Success报告进程是否成功退出,如在Unix里以状态码0退出。 ### func (\*ProcessState) [SystemTime](https://github.com/golang/go/blob/master/src/os/doc.go#L60 "View Source") ``` func (p *ProcessState) SystemTime() time.Duration ``` SystemTime返回已退出进程及其子进程耗费的系统CPU时间。 ### func (\*ProcessState) [UserTime](https://github.com/golang/go/blob/master/src/os/doc.go#L55 "View Source") ``` func (p *ProcessState) UserTime() time.Duration ``` UserTime返回已退出进程及其子进程耗费的用户CPU时间。 ### func (\*ProcessState) [Sys](https://github.com/golang/go/blob/master/src/os/doc.go#L78 "View Source") ``` func (p *ProcessState) Sys() interface{} ``` Sys返回该已退出进程系统特定的退出信息。需要将其类型转换为适当的底层类型,如Unix里转换为\*syscall.WaitStatus类型以获取其内容。 ### func (\*ProcessState) [SysUsage](https://github.com/golang/go/blob/master/src/os/doc.go#L87 "View Source") ``` func (p *ProcessState) SysUsage() interface{} ``` SysUsage返回该已退出进程系统特定的资源使用信息。需要将其类型转换为适当的底层类型,如Unix里转换为\*syscall.Rusage类型以获取其内容。 ### func (\*ProcessState) [String](https://github.com/golang/go/blob/master/src/os/exec_posix.go#L111 "View Source") ``` func (p *ProcessState) String() string ```