天天看点

Go语言从入门到规范-6.2、Go语言os包Go语言从入门到规范-6.2、Go语言os包

Go语言从入门到规范-6.2、Go语言os包

文章目录

  • Go语言从入门到规范-6.2、Go语言os包
    • 1.概述
    • 2.常量
    • 3.变量
    • 4.func [Chdir](https://go-zh.org/src/os/file.go?s=6322:6350#L210)
    • 5.func [Chmod](https://go-zh.org/src/os/file_posix.go?s=1434:1478#L48)
    • 6.func [Chown](https://go-zh.org/src/os/file_posix.go?s=2101:2144#L70)
    • 7.func [Chtimes](https://go-zh.org/src/os/file_posix.go?s=3867:3932#L131)
    • 8.func [Clearenv](https://go-zh.org/src/os/env.go?s=2854:2869#L90)
    • 9.func [Environ](https://go-zh.org/src/os/env.go?s=2990:3013#L96)
    • 10.func [Exit](https://go-zh.org/src/os/proc.go?s=1328:1347#L39)
    • 11.func [Expand](https://go-zh.org/src/os/env.go?s=379:436#L3)
    • 12.func [ExpandEnv](https://go-zh.org/src/os/env.go?s=963:994#L22)
    • 13.func [Getegid](https://go-zh.org/src/os/proc.go?s=867:885#L27)
    • 14.func [Getenv](https://go-zh.org/src/os/env.go?s=2363:2393#L69)
    • 15.func [Geteuid](https://go-zh.org/src/os/proc.go?s=652:670#L21)
    • 16.func [Getgid](https://go-zh.org/src/os/proc.go?s=755:772#L24)
    • 17.func [Getgroups](https://go-zh.org/src/os/proc.go?s=1001:1032#L30)
    • 18.func [Getpagesize](https://go-zh.org/src/os/types.go?s=268:290#L3)
    • 19.func [Getpid](https://go-zh.org/src/os/exec.go?s=2009:2026#L57)
    • 20.func [Getppid](https://go-zh.org/src/os/exec.go?s=2114:2132#L60)
    • 21.func [Getuid](https://go-zh.org/src/os/proc.go?s=541:558#L18)
    • 22.func [Getwd](https://go-zh.org/src/os/getwd.go?s=621:657#L16)
    • 23.func [Hostname](https://go-zh.org/src/os/doc.go?s=2934:2974#L82)
    • 24.func [IsExist](https://go-zh.org/src/os/error.go?s=1387:1415#L39)
    • 25.func [IsNotExist](https://go-zh.org/src/os/error.go?s=1632:1663#L46)
    • 26.func [IsPathSeparator](https://go-zh.org/src/os/path_unix.go?s=441:475#L5)
    • 27.func [IsPermission](https://go-zh.org/src/os/error.go?s=1873:1906#L53)
    • 28.func [Lchown](https://go-zh.org/src/os/file_posix.go?s=2453:2497#L80)
    • 29.func [Link](https://go-zh.org/src/os/file_unix.go?s=9281:9321#L325)
    • 30.func [Mkdir](https://go-zh.org/src/os/file.go?s=5864:5908#L194)
    • 31.func [MkdirAll](https://go-zh.org/src/os/path.go?s=488:535#L9)
    • 32.func [NewSyscallError](https://go-zh.org/src/os/error.go?s=1075:1128#L29)
    • 33.func [Readlink](https://go-zh.org/src/os/file_posix.go?s=451:493#L8)
    • 34.func [Remove](https://go-zh.org/src/os/file_unix.go?s=7726:7756#L263)
    • 35.func [RemoveAll](https://go-zh.org/src/os/path.go?s=1635:1668#L56)
    • 36.func [Rename](https://go-zh.org/src/os/file.go?s=7581:7623#L251)
    • 37.func [SameFile](https://go-zh.org/src/os/types.go?s=3880:3917#L101)
    • 38.func [Setenv](https://go-zh.org/src/os/env.go?s=2541:2577#L76)
    • 39.func [Symlink](https://go-zh.org/src/os/file_unix.go?s=9557:9600#L335)
    • 40.func [TempDir](https://go-zh.org/src/os/file_unix.go?s=8991:9012#L311)
    • 41.func [Truncate](https://go-zh.org/src/os/file_unix.go?s=7467:7511#L254)
    • 42.func [Unsetenv](https://go-zh.org/src/os/env.go?s=2740:2771#L85)
    • 43.type [File](https://go-zh.org/src/os/file_unix.go?s=335:362#L6)
      • (1).func [Create](https://go-zh.org/src/os/file.go?s=7349:7397#L243)
      • (2).func [NewFile](https://go-zh.org/src/os/file_unix.go?s=1063:1106#L31)
      • (3).func [Open](https://go-zh.org/src/os/file.go?s=6987:7033#L234)
      • (4).func [OpenFile](https://go-zh.org/src/os/file_unix.go?s=2142:2217#L67)
      • (5).func [Pipe](https://go-zh.org/src/os/pipe_linux.go?s=319:360#L1)
      • (6).func (*File) [Chdir](https://go-zh.org/src/os/file.go?s=6594:6622#L220)
      • (7).func (*File) [Chmod](https://go-zh.org/src/os/file_posix.go?s=1700:1741#L57)
      • (8).func (*File) [Chown](https://go-zh.org/src/os/file_posix.go?s=2725:2765#L89)
      • (9).func (*File) [Close](https://go-zh.org/src/os/file_unix.go?s=2933:2961#L96)
      • (10).func (*File) [Fd](https://go-zh.org/src/os/file_unix.go?s=896:923#L23)
      • (11).func (*File) [Name](https://go-zh.org/src/os/file.go?s=1531:1559#L35)
      • (12).func (*File) [Read](https://go-zh.org/src/os/file.go?s=3214:3262#L81)
      • (13).func (*File) [ReadAt](https://go-zh.org/src/os/file.go?s=3716:3777#L102)
      • (14).func (*File) [Readdir](https://go-zh.org/src/os/doc.go?s=3807:3863#L101)
      • (15).func (*File) [Readdirnames](https://go-zh.org/src/os/doc.go?s=4579:4641#L120)
      • (16).func (*File) [Seek](https://go-zh.org/src/os/file.go?s=5209:5277#L169)
      • (17).func (*File) [Stat](https://go-zh.org/src/os/file_unix.go?s=3468:3514#L120)
      • (18).func (*File) [Sync](https://go-zh.org/src/os/file_posix.go?s=3418:3451#L115)
      • (19).func (*File) [Truncate](https://go-zh.org/src/os/file_posix.go?s=3052:3093#L102)
      • (20).func (*File) [Write](https://go-zh.org/src/os/file.go?s=4193:4242#L125)
      • (21).func (*File) [WriteAt](https://go-zh.org/src/os/file.go?s=4659:4721#L148)
      • (22).func (*File) [WriteString](https://go-zh.org/src/os/file.go?s=5599:5656#L185)
    • 44.type [FileInfo](https://go-zh.org/src/os/types.go?s=391:752#L6)
      • (1).func [Lstat](https://go-zh.org/src/os/file_unix.go?s=4305:4353#L147)
      • (2).func [Stat](https://go-zh.org/src/os/file_unix.go?s=3843:3890#L134)
    • 45.type [FileMode](https://go-zh.org/src/os/types.go?s=1044:1064#L20)
      • (1).func (FileMode) [IsDir](https://go-zh.org/src/os/types.go?s=3070:3100#L77)
      • (2).func (FileMode) [IsRegular](https://go-zh.org/src/os/types.go?s=3239:3273#L83)
      • (3).func (FileMode) [Perm](https://go-zh.org/src/os/types.go?s=3350:3383#L88)
      • (4).func (FileMode) [String](https://go-zh.org/src/os/types.go?s=2558:2591#L49)
    • 46.type [LinkError](https://go-zh.org/src/os/file.go?s=2868:2940#L67)
      • (1).func (*LinkError) [Error](https://go-zh.org/src/os/file.go?s=2942:2976#L74)
    • 47.type [PathError](https://go-zh.org/src/os/error.go?s=535:598#L10)
      • (1).func (*PathError) [Error](https://go-zh.org/src/os/error.go?s=600:634#L16)
    • 48.type [ProcAttr](https://go-zh.org/src/os/exec.go?s=818:1712#L26)
    • 49.type [Process](https://go-zh.org/src/os/exec.go?s=296:423#L4)
      • (1).func [FindProcess](https://go-zh.org/src/os/doc.go?s=352:401#L2)
      • (2).func [StartProcess](https://go-zh.org/src/os/doc.go?s=710:789#L13)
      • (3).func (*Process) [Kill](https://go-zh.org/src/os/doc.go?s=1097:1127#L25)
      • (4).func (*Process) [Release](https://go-zh.org/src/os/doc.go?s=990:1023#L20)
      • (5).func (*Process) [Signal](https://go-zh.org/src/os/doc.go?s=1605:1647#L40)
      • (6).func (*Process) [Wait](https://go-zh.org/src/os/doc.go?s=1442:1489#L34)
    • 50.type [ProcessState](https://go-zh.org/src/os/exec_posix.go?s=1457:1617#L47)
      • (1).func (*ProcessState) [Exited](https://go-zh.org/src/os/doc.go?s=2037:2073#L55)
      • (2).func (*ProcessState) [Pid](https://go-zh.org/src/os/exec_posix.go?s=1672:1704#L54)
      • (3).func (*ProcessState) [String](https://go-zh.org/src/os/exec_posix.go?s=2001:2039#L74)
      • (4).func (*ProcessState) [Success](https://go-zh.org/src/os/doc.go?s=2197:2234#L61)
      • (5).func (*ProcessState) [Sys](https://go-zh.org/src/os/doc.go?s=2442:2482#L68)
      • (6).func (*ProcessState) [SysUsage](https://go-zh.org/src/os/doc.go?s=2804:2849#L77)
      • (8).func (*ProcessState) [SystemTime](https://go-zh.org/src/os/doc.go?s=1909:1958#L50)
      • (9).func (*ProcessState) [UserTime](https://go-zh.org/src/os/doc.go?s=1753:1800#L45)
    • 51.type [Signal](https://go-zh.org/src/os/exec.go?s=1868:1959#L51)
    • 52.type [SyscallError](https://go-zh.org/src/os/error.go?s=752:811#L19)
      • (1).func (*SyscallError) [Error](https://go-zh.org/src/os/error.go?s=813:850#L24)

以下内容主要来自:https://go-zh.org/pkg/os/,部分内容通过实践进行总结。

主要是熟悉以下相关接口,在实际开发过程中根据需要可以找到,不至于卡住开发思路。

1.概述

Package os提供了一个独立于平台的操作系统功能接口。

该设计类似于unix,尽管错误处理类似于go;失败的调用返回的是error类型的值,而不是错误号。通常,在错误中可以得到更多的信息。例如,如果接受文件名的调用失败,例如Open或Stat,则在打印时错误将包括失败的文件名,并且类型为

*PathError

,可以解包以获取更多信息。

os接口在所有操作系统中是统一的。特定于系统的包sycall中出现了一般不可用的特性。

下面是一个简单的例子,打开一个文件并读取其中的一些内容。

file, err := os.Open("file.go") // For read access.
if err != nil {
    log.Fatal(err)
}
           

如果打开失败,错误字符串将是不言自明的,例如

open file.go: no such file or directory
           

然后可以将文件的数据读入字节片。读和写的字节数取自参数slice的长度。

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])
           

一些源码包:dir_unix.go doc.go env.go error.go error_unix.go exec.go exec_posix.go exec_unix.go file.go file_posix.go file_unix.go getwd.go path.go path_unix.go pipe_linux.go proc.go stat_linux.go sticky_notbsd.go str.go sys_linux.go sys_unix.go types.go types_notwin.go

2.常量

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 (
    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位置。

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

DevNull是操作系统的“空设备”的名称。在类unix系统中,它是"/dev/null";在Windows上,“NUL”。

3.变量

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是指向标准输入、标准输出和标准错误文件描述符的打开文件。

Args保存命令行参数,从程序名开始。

4.func Chdir

Chdir将当前工作目录更改为命名目录。如果有错误,它的类型是*PathError。

5.func Chmod

Chmod修改命名文件的模式为mode。如果文件是一个符号链接,它会改变链接目标的模式。如果有错误,它的类型是*PathError。

6.func Chown

Chown修改命名文件的数字uid和gid。如果文件是一个符号链接,它将更改链接目标的uid和gid。如果有错误,它的类型是*PathError。

7.func Chtimes

Chtimes修改命名文件的访问和修改时间,类似于Unix的utime()或utimes()函数。

底层文件系统可能会将值截断或舍入到一个不那么精确的时间单位。如果有错误,它的类型是*PathError。

8.func Clearenv

Clearenv删除所有环境变量。

9.func Environ

Environ返回一个表示环境的字符串副本,格式为"key=value"。

10.func Exit

Exit使当前程序以给定的状态码退出。按照惯例,代码0表示成功,非0表示错误。程序立即终止;延迟函数不运行。

11.func Expand

Expand根据映射函数替换字符串中的 v a r 或 {var}或 var或var。例如,os. expandenv (s)等价于os。扩大(年代,os.Getenv)。

12.func ExpandEnv

ExpandEnv根据当前环境变量的值替换字符串中的 v a r 或 {var}或 var或var。对未定义变量的引用被替换为空字符串。

13.func Getegid

Getegid返回调用者的数字有效组id。

14.func Getenv

Getenv检索key命名的环境变量的值。它返回值,如果变量不存在,该值将为空。

15.func Geteuid

Geteuid返回调用者的数字有效用户id。

16.func Getgid

Getgid返回调用者的数字组id.

17.func Getgroups

Getgroups返回调用者所属组的数字id列表。

18.func Getpagesize

返回底层系统的内存页大小。

19.func Getpid

Getpid返回调用者的进程id。

20.func Getppid

Getppid返回调用者父进程的进程id.

21.func Getuid

Getuid返回调用者的数字用户id。

22.func Getwd

Getwd返回与当前目录对应的根路径名。如果当前目录可以通过多个路径到达(由于符号链接),Getwd可以返回其中的任何一个路径。

23.func Hostname

Hostname返回内核报告的主机名.

24.func IsExist

IsExist返回一个布尔值,指示是否已知错误,以报告文件或目录已经存在。它满足于ErrExist和一些系统调用错误。

25.func IsNotExist

isnoexistist返回一个布尔值,指示是否已知错误,并报告文件或目录不存在。ErrNotExist和一些系统调用错误满足了这个要求。

26.func IsPathSeparator

如果c是目录分隔符,则IsPathSeparator返回true。

27.func IsPermission

IsPermission返回一个布尔值,指示是否已知错误,以报告权限被拒绝。它由ErrPermission和一些系统调用错误来满足.

28.func Lchown

Lchown修改命名文件的数字型uid和gid。如果文件是一个符号链接,它会更改链接本身的uid和gid。如果有错误,它的类型是*PathError。

29.func Link

Link创建newname作为到oldname文件的硬链接。如果有错误,它的类型是*LinkError。

30.func Mkdir

Mkdir使用指定的名称和权限位创建新目录。如果有错误,它的类型是*PathError。

31.func MkdirAll

MkdirAll创建一个名为path的目录,以及任何必要的父目录,并返回nil,否则返回一个错误。perm权限位用于MkdirAll创建的所有目录。如果path已经是一个目录,MkdirAll将不做任何操作并返回nil。

32.func NewSyscallError

NewSyscallError以错误的形式返回一个新的SyscallError,它具有给定的系统调用名称和错误详细信息。为方便起见,如果err为nil, NewSyscallError返回nil。

33.func Readlink

Readlink返回指定符号链接的目的地。如果有错误,它的类型是*PathError。

34.func Remove

Remove删除指定的文件或目录。如果有错误,它的类型是*PathError。

35.func RemoveAll

RemoveAll删除路径及其包含的任何子路径。它删除它所能删除的所有内容,但返回它遇到的第一个错误。如果路径不存在,RemoveAll返回nil(没有错误)。

36.func Rename

Rename重命名(移动)一个文件。特定于操作系统的限制可能适用.

37.func SameFile

SameFile报告fi1和fi2是否描述同一个文件。例如,在Unix上,这意味着两个底层结构的device和inode字段是相同的;在其他系统上,决策可能基于路径名。SameFile只应用于该包的Stat返回的结果。在其他情况下它返回false。

38.func Setenv

Setenv设置key命名的环境变量的值。它返回一个错误(如果有的话)。

39.func Symlink

Symlink创建newname作为到oldname的符号链接。如果有错误,它的类型是*LinkError。

40.func TempDir

TempDir返回用于临时文件的默认目录。

41.func Truncate

Truncate更改命名文件的大小。如果文件是一个符号链接,它会改变链接目标的大小。如果有错误,它的类型是*PathError。

42.func Unsetenv

Unsetenv取消设置单个环境变量.

43.type File

type File struct {
    // contains filtered or unexported fields
}
           

File表示打开的文件描述符。

(1).func Create

Create创建命名文件模式0666(在umask之前),如果它已经存在,则将其截断。如果成功,返回文件的方法可以用于I/O;相关的文件描述符具有O_RDWR模式。如果有错误,它的类型是*PathError。

(2).func NewFile

func NewFile(fd uintptr, name string) *File
           

NewFile返回一个具有给定文件描述符和名称的新文件。

(3).func Open

打开指定文件以供读取。如果成功,返回文件的方法可以用于读取;相关的文件描述符具有O_RDONLY模式。如果有错误,它的类型是*PathError。

(4).func OpenFile

OpenFile是广义的开放调用;大多数用户将使用Open或Create代替。它以指定的标志(O_RDONLY等)和perm(0666等)打开命名文件(如果适用的话)。如果成功,返回文件上的方法可以用于I/O。如果有错误,它的类型是*PathError。

(5).func Pipe

Pipe返回一个连接的文件对;从r读取,返回写入w的字节。它返回文件和一个错误(如果有的话)。

(6).func (*File) Chdir

Chdir将当前工作目录更改为文件,该文件必须是一个目录。如果有错误,它的类型是*PathError.

(7).func (*File) Chmod

Chmod修改文件的模式为mode。如果有错误,它的类型是*PathError。

(8).func (*File) Chown

Chown修改命名文件的数字uid和gid。如果有错误,它的类型是*PathError。

(9).func (*File) Close

Close关闭文件,使其无法用于I/O。它返回一个错误(如果有的话)。

(10).func (*File) Fd

Fd返回引用打开文件的整数Unix文件描述符。文件描述符仅在f.close被调用或f被垃圾收集之前有效。

(11).func (*File) Name

Name返回打开的文件的名称。

(12).func (*File) Read

Read从文件中读取len(b)字节。它返回读取的字节数和一个错误(如果有的话)。EOF的信号是零计数,err设置为io.EOF。

(13).func (*File) ReadAt

ReadAt从文件的字节偏移量off开始读取len(b)字节。它返回读取的字节数和错误(如果有的话)。当n < len(b)时,ReadAt总是返回一个非空错误。文件末尾的错误是io.EOF。

(14).func (*File) Readdir

Readdir读取与file相关的目录内容,并返回一个片,最多包含n个FileInfo值,就像Lstat按照目录顺序返回的那样。后续对同一文件的调用将产生更多的文件信息。

如果n > 0, Readdir最多返回n个FileInfo结构。在这种情况下,如果Readdir返回一个空片,它将返回一个非空错误解释原因。在目录的末尾,错误是io.EOF。

如果n <= 0, Readdir将从单个切片中返回该目录的所有FileInfo。在本例中,如果Readdir成功(一直读取到目录的末尾),它将返回切片和nil错误。如果它在目录结束之前遇到错误,Readdir将返回该点之前读取的文件信息和一个非空错误。

(15).func (*File) Readdirnames

Readdirnames从目录f读取并返回一个名称片。

如果n > 0, Readdirnames最多返回n个名称。在这种情况下,如果Readdirnames返回一个空片,它将返回一个非空错误解释原因。在目录的末尾,错误是io.EOF。

如果n <= 0, Readdirnames返回单个片中目录中的所有名称。在本例中,如果Readdirnames成功(一直读取到目录的末尾),它将返回切片和nil错误。如果它在目录结束之前遇到错误,Readdirnames将返回在该点之前读取的名称和一个非空错误。

(16).func (*File) Seek

Seek将下一个读或写文件的偏移量设置为偏移量,根据偏移量解释:0表示相对于文件的原点,1表示相对于当前偏移量,2表示相对于末尾。它返回新的偏移量和一个错误(如果有的话)。

(17).func (*File) Stat

Stat返回描述文件的FileInfo结构。如果有错误,它的类型是*PathError。

(18).func (*File) Sync

Sync将文件的当前内容提交到稳定存储。通常,这意味着刷新文件系统中最近写入到磁盘的数据的内存副本。

(19).func (*File) Truncate

Truncate更改文件的大小。它不会改变I/O偏移量。如果有错误,它的类型是*PathError。

(20).func (*File) Write

Write将len(b)字节写入文件。它返回已写入的字节数和一个错误(如果有的话)。当n != len(b)时,Write返回一个非空错误。

(21).func (*File) WriteAt

WriteAt从字节偏移量off开始将len(b)字节写入文件。它返回已写入的字节数和一个错误(如果有的话)。当n != len(b)时,WriteAt返回一个非空错误。

(22).func (*File) WriteString

WriteString与Write类似,但写入字符串s的内容,而不是字节片。

44.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)
}
           

FileInfo描述一个文件,由Stat和Lstat返回。

(1).func Lstat

Lstat返回一个描述命名文件的文件信息。如果文件是一个符号链接,则返回的FileInfo描述这个符号链接。Lstat没有尝试跟踪该链接。如果有错误,它的类型是*PathError

(2).func Stat

Stat返回一个描述命名文件的文件信息。如果有错误,它的类型是*PathError。

45.type FileMode

文件模式表示文件的模式位和权限位。位在所有系统上具有相同的定义,因此有关文件的信息可以从一个系统移动到另一个系统。并不是所有的bits都适用于所有的系统。唯一需要的位是目录的ModeDir。

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 (not backed up)
    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 // permission bits
)
           

定义的FileMode位是文件模式的最有效位。最低有效位是标准的Unix rwxrwxrwx权限。这些位的值应该被认为是公共API的一部分,并且可以在有线协议或磁盘表示中使用:它们不能被更改,尽管可能会添加新的位。

(1).func (FileMode) IsDir

IsDir报告m是否描述目录。也就是说,它测试m中是否设置了moddir位。

(2).func (FileMode) IsRegular

IsRegular报告m是否描述常规文件。也就是说,它测试没有设置模式类型位。

(3).func (FileMode) Perm

func (m FileMode) Perm() FileMode
           

Perm返回m中的Unix权限位。

(4).func (FileMode) String

46.type LinkError

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

LinkError记录在链接、符号链接或重命名系统调用期间的错误以及导致错误的路径

(1).func (*LinkError) Error

47.type PathError

type PathError struct {
    Op   string
    Path string
    Err  error
}
           

PathError记录错误以及导致错误的操作和文件路径。

(1).func (*PathError) Error

48.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保存了将应用于由StartProcess启动的新进程的属性。

49.type Process

type Process struct {
    Pid int
    // contains filtered or unexported fields
}
           

Process存储StartProcess创建的进程信息.

(1).func FindProcess

FindProcess根据进程的pid查找正在运行的进程。它返回的进程可用于获取有关底层操作系统进程的信息。

(2).func StartProcess

StartProcess使用程序、参数和由name、argv和attr指定的属性启动一个新进程。

StartProcess是一个低级接口。os/exec包提供了更高级的接口。

如果有错误,它的类型是*PathError。

(3).func (*Process) Kill

Kill导致进程立即退出。

(4).func (*Process) Release

Release释放与流程p相关的所有资源,使其在未来不可用。只有在没有Wait的情况下才需要调用Release.

(5).func (*Process) Signal

Signal向进程发送信号。在Windows上不实现发送中断

(6).func (*Process) Wait

Wait等待进程退出,然后返回描述其状态的ProcessState和错误(如果有的话)。Wait释放与该进程相关的所有资源。在大多数操作系统中,进程必须是当前进程的子进程,否则将返回错误。

50.type ProcessState

type ProcessState struct {
    // contains filtered or unexported fields
}
           

ProcessState存储关于进程的信息,如Wait所报告的那样。

(1).func (*ProcessState) Exited

Exited报告程序是否已经退出.

(2).func (*ProcessState) Pid

Pid返回退出进程的进程id.

(3).func (*ProcessState) String

(4).func (*ProcessState) Success

Success报告程序是否成功退出,例如在Unix上退出状态为0。

(5).func (*ProcessState) Sys

返回与系统相关的进程退出信息。将其转换为适当的底层类型,例如系统调用。在Unix上的WaitStatus,以访问其内容。

(6).func (*ProcessState) SysUsage

SysUsage返回与退出进程相关的系统资源使用信息。将其转换为适当的底层类型,例如*syscall。以访问它的内容。(在Unix系统调用。与getrusage(2)手册页中定义的struct Rusage匹配。)

(8).func (*ProcessState) SystemTime

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

SystemTime返回退出进程及其子进程的系统CPU时间.

(9).func (*ProcessState) UserTime

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

UserTime返回退出的进程及其子进程的用户CPU时间。

51.type Signal

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

一个信号代表一个操作系统信号。通常的底层实现是依赖于操作系统的:在Unix上是sycall.signal。

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

唯一保证在所有系统上存在的信号值是Interrupt(给进程发送一个中断)和Kill(强制进程退出)。

52.type SyscallError

type SyscallError struct {
    Syscall string
    Err     error
}
           

SyscallError记录来自特定系统调用的错误。

(1).func (*SyscallError) Error

go