# 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>0,Readdir函數會返回一個最多n個成員的切片。這時,如果Readdir返回一個空切片,它會返回一個非nil的錯誤說明原因。如果到達了目錄f的結尾,返回值err會是io.EOF。
如果n<=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>0,Readdir函數會返回一個最多n個成員的切片。這時,如果Readdir返回一個空切片,它會返回一個非nil的錯誤說明原因。如果到達了目錄f的結尾,返回值err會是io.EOF。
如果n<=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<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
```
- 庫
- package achive
- package tar
- package zip
- package bufio
- package builtin
- package bytes
- package compress
- package bzip2
- package flate
- package gzip
- package lzw
- package zlib
- package container
- package heap
- package list
- package ring
- package crypto
- package aes
- package cipher
- package des
- package dsa
- package ecdsa
- package elliptic
- package hmac
- package md5
- package rand
- package rc4
- package rsa
- package sha1
- package sha256
- package sha512
- package subtle
- package tls
- package x509
- package pkix
- package database
- package sql
- package driver
- package encoding
- package ascii85
- package asn1
- package base32
- package base64
- package binary
- package csv
- package gob
- package hex
- package json
- package pem
- package xml
- package errors
- package expvar
- package flag
- package fmt
- package go
- package doc
- package format
- package parser
- package printer
- package hash
- package adler32
- package crc32
- package crc64
- package fnv
- package html
- package template
- package image
- package color
- package palette
- package draw
- package gif
- package jpeg
- package png
- package index
- package suffixarray
- package io
- package ioutil
- package log
- package syslog
- package math
- package big
- package cmplx
- package rand
- package mime
- package multipart
- package net
- package http
- package cgi
- package cookiejar
- package fcgi
- package httptest
- package httputil
- package pprof
- package mail
- package rpc
- package jsonrpc
- package smtp
- package textproto
- package url
- package os
- package exec
- package signal
- package user
- package path
- package filepath
- package reflect
- package regexp
- package runtime
- package cgo
- package debug
- package pprof
- package race
- package sort
- package strconv
- package strings
- package sync
- package atomic
- package text
- package scanner
- package tabwriter
- package template
- package time
- package unicode
- package utf16
- package utf8
- package unsafe