天天看點

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