?
本文檔使用 php中文網(wǎng)手冊(cè) 發(fā)布
import "os"
Overview
Index
Examples
Subdirectories
Package os為操作系統(tǒng)功能提供了一個(gè)平臺(tái)無(wú)關(guān)的接口。雖然錯(cuò)誤處理類(lèi)似于 Go,但設(shè)計(jì)類(lèi)似 Unix,失敗的調(diào)用返回類(lèi)型錯(cuò)誤的值而不是錯(cuò)誤號(hào)。錯(cuò)誤中通常會(huì)提供更多信息。例如,如果接收文件名的調(diào)用失敗(例如 Open 或 Stat ),則錯(cuò)誤將在打印時(shí)包含失敗的文件名,并且將是 * PathError 類(lèi)型,可能會(huì)解壓縮以獲取更多信息。
OS 界面旨在使所有操作系統(tǒng)均勻。通常不可用的功能出現(xiàn)在系統(tǒng)特定的軟件包 syscall 中。
這里有一個(gè)簡(jiǎn)單的例子,打開(kāi)一個(gè)文件并閱讀一些文件。
file, err := os.Open("file.go") // For read access.if err != nil { log.Fatal(err)}
如果打開(kāi)失敗,錯(cuò)誤字符串將不言自明,如
open file.go: no such file or directory
然后可以將文件的數(shù)據(jù)讀入一段字節(jié)。讀取和寫(xiě)入從參數(shù)切片的長(zhǎng)度獲取其字節(jié)數(shù)。
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])
Constants
Variables
func Chdir(dir string) error
func Chmod(name string, mode FileMode) error
func Chown(name string, uid, gid int) error
func Chtimes(name string, atime time.Time, mtime time.Time) error
func Clearenv()
func Environ() []string
func Executable() (string, error)
func Exit(code int)
func Expand(s string, mapping func(string) string) string
func ExpandEnv(s string) string
func Getegid() int
func Getenv(key string) string
func Geteuid() int
func Getgid() int
func Getgroups() ([]int, error)
func Getpagesize() int
func Getpid() int
func Getppid() int
func Getuid() int
func Getwd() (dir string, err error)
func Hostname() (name string, err error)
func IsExist(err error) bool
func IsNotExist(err error) bool
func IsPathSeparator(c uint8) bool
func IsPermission(err error) bool
func Lchown(name string, uid, gid int) error
func Link(oldname, newname string) error
func LookupEnv(key string) (string, bool)
func Mkdir(name string, perm FileMode) error
func MkdirAll(path string, perm FileMode) error
func NewSyscallError(syscall string, err error) error
func Readlink(name string) (string, error)
func Remove(name string) error
func RemoveAll(path string) error
func Rename(oldpath, newpath string) error
func SameFile(fi1, fi2 FileInfo) bool
func Setenv(key, value string) error
func Symlink(oldname, newname string) error
func TempDir() string
func Truncate(name string, size int64) error
func Unsetenv(key string) error
type File
func Create(name string) (*File, error)
func NewFile(fd uintptr, name string) *File
func Open(name string) (*File, error)
func OpenFile(name string, flag int, perm FileMode) (*File, error)
func Pipe() (r *File, w *File, err error)
func (f *File) Chdir() error
func (f *File) Chmod(mode FileMode) error
func (f *File) Chown(uid, gid int) error
func (f *File) Close() error
func (f *File) Fd() uintptr
func (f *File) Name() string
func (f *File) Read(b []byte) (n int, err error)
func (f *File) ReadAt(b []byte, off int64) (n int, err error)
func (f *File) Readdir(n int) ([]FileInfo, error)
func (f *File) Readdirnames(n int) (names []string, err error)
func (f *File) Seek(offset int64, whence int) (ret int64, err error)
func (f *File) Stat() (FileInfo, error)
func (f *File) Sync() error
func (f *File) Truncate(size int64) error
func (f *File) Write(b []byte) (n int, err error)
func (f *File) WriteAt(b []byte, off int64) (n int, err error)
func (f *File) WriteString(s string) (n int, err error)
type FileInfo
func Lstat(name string) (FileInfo, error)
func Stat(name string) (FileInfo, error)
type FileMode
func (m FileMode) IsDir() bool
func (m FileMode) IsRegular() bool
func (m FileMode) Perm() FileMode
func (m FileMode) String() string
type LinkError
func (e *LinkError) Error() string
type PathError
func (e *PathError) Error() string
type ProcAttr
type Process
func FindProcess(pid int) (*Process, error)
func StartProcess(name string, argv []string, attr *ProcAttr) (*Process, error)
func (p *Process) Kill() error
func (p *Process) Release() error
func (p *Process) Signal(sig Signal) error
func (p *Process) Wait() (*ProcessState, error)
type ProcessState
func (p *ProcessState) Exited() bool
func (p *ProcessState) Pid() int
func (p *ProcessState) String() string
func (p *ProcessState) Success() bool
func (p *ProcessState) Sys() interface{}
func (p *ProcessState) SysUsage() interface{}
func (p *ProcessState) SystemTime() time.Duration
func (p *ProcessState) UserTime() time.Duration
type Signal
type SyscallError
func (e *SyscallError) Error() string
Chmod Chtimes ExpandEnv FileMode Getenv IsNotExist LookupEnv OpenFile OpenFile (Append) Unsetenv
dir.go dir_unix.go env.go error.go error_posix.go error_unix.go exec.go exec_posix.go exec_unix.go executable.go executable_procfs.go file.go file_posix.go file_unix.go getwd.go path.go path_unix.go pipe_linux.go proc.go stat_linux.go stat_unix.go sticky_notbsd.go str.go sys.go sys_linux.go sys_unix.go types.go types_unix.go wait_waitid.go
標(biāo)記為 OpenFile 封裝底層系統(tǒng)的標(biāo)記。并非所有的標(biāo)志都可以在給定的系統(tǒng)上實(shí)現(xiàn)。
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.)
尋求哪些價(jià)值。
Deprecated: Use io.SeekStart, io.SeekCurrent, and io.SeekEnd.
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)
const ( PathSeparator = '/' // OS-specific path separator PathListSeparator = ':' // OS-specific path list separator)
DevNull 是操作系統(tǒng)“空設(shè)備”的名稱(chēng)。在類(lèi) Unix 系統(tǒng)上,它是“/ dev / null”; 在Windows上,“NUL”。
const DevNull = "/dev/null"
一些常見(jiàn)系統(tǒng)調(diào)用錯(cuò)誤的便攜式模擬器。
var ( ErrInvalid = errors.New("invalid argument") // methods on File will return this error when the receiver is nil ErrPermission = errors.New("permission denied") ErrExist = errors.New("file already exists") ErrNotExist = errors.New("file does not exist") ErrClosed = errors.New("file already closed"))
Stdin,Stdout 和 Stderr 是打開(kāi)的文件,指向標(biāo)準(zhǔn)輸入,標(biāo)準(zhǔn)輸出和標(biāo)準(zhǔn)錯(cuò)誤文件描述符。
請(qǐng)注意,Go 運(yùn)行時(shí)寫(xiě)入標(biāo)準(zhǔn)錯(cuò)誤以防恐慌和崩潰;關(guān)閉 Stderr 可能會(huì)導(dǎo)致這些消息到其他地方,也許會(huì)導(dǎo)致稍后打開(kāi)的文件。
var ( Stdin = NewFile(uintptr(syscall.Stdin), "/dev/stdin") Stdout = NewFile(uintptr(syscall.Stdout), "/dev/stdout") Stderr = NewFile(uintptr(syscall.Stderr), "/dev/stderr"))
參數(shù)從程序名稱(chēng)開(kāi)始保存命令行參數(shù)。
var Args []string
func Chdir(dir string) error
Chdir 將當(dāng)前工作目錄更改為指定的目錄。如果有錯(cuò)誤,它將是 * PathError 類(lèi)型。
func Chmod(name string, mode FileMode) error
Chmod 將指定文件的模式更改為模式。如果文件是符號(hào)鏈接,它將更改鏈接目標(biāo)的模式。如果有錯(cuò)誤,它將是 * PathError 類(lèi)型。
取決于操作系統(tǒng),使用模式位的不同子集。
在 Unix 上,使用模式的權(quán)限位 ModeSetuid,ModeSetgid 和 ModeSticky 。
在 Windows 上,模式必須是非零的,否則只能使用0200位(所有者可寫(xiě))模式; 它控制文件的只讀屬性是設(shè)置還是清除。屬性。其他位當(dāng)前未使用。對(duì)于只讀文件使用模式0400,對(duì)于可讀+可寫(xiě)文件使用0600。
在計(jì)劃9中,使用模式的許可位 ModeAppend,ModeExclusive 和 ModeTemporary 。
package mainimport ("log""os")func main() {if err := os.Chmod("some-filename", 0644); err != nil { log.Fatal(err)}}
func Chown(name string, uid, gid int) error
Chown 更改指定文件的數(shù)字 uid 和 gid 。如果該文件是符號(hào)鏈接,它會(huì)更改鏈接目標(biāo)的 uid 和 gid 。如果有錯(cuò)誤,它將是 * PathError 類(lèi)型。
在 Windows 上,它總是返回包裝在 * PathError 中的 syscall.EWINDOWS 錯(cuò)誤。
func Chtimes(name string, atime time.Time, mtime time.Time) error
Chtimes 更改指定文件的訪問(wèn)和修改時(shí)間,類(lèi)似于 Unix utime() 或 utimes() 函數(shù)。
底層文件系統(tǒng)可能會(huì)將值截?cái)嗷蛏崛氲讲惶_的時(shí)間單位。如果有錯(cuò)誤,它將是 * PathError 類(lèi)型。
package mainimport ("log""os""time")func main() { mtime := time.Date(2006, time.February, 1, 3, 4, 5, 0, time.UTC) atime := time.Date(2007, time.March, 2, 4, 5, 6, 0, time.UTC)if err := os.Chtimes("some-filename", atime, mtime); err != nil { log.Fatal(err)}}
func Clearenv()
Clearenv 刪除所有環(huán)境變量。
func Environ() []string
Environ 以 “key = value” 的形式返回表示環(huán)境的字符串的副本。
func Executable() (string, error)
可執(zhí)行文件返回啟動(dòng)當(dāng)前進(jìn)程的可執(zhí)行文件的路徑名稱(chēng)。不能保證路徑仍然指向正確的可執(zhí)行文件。如果使用符號(hào)鏈接來(lái)啟動(dòng)進(jìn)程,則根據(jù)操作系統(tǒng)的不同,結(jié)果可能是符號(hào)鏈接或它指向的路徑。如果需要穩(wěn)定的結(jié)果, path / filepath.EvalSymlinks 可能會(huì)有所幫助。
除非發(fā)生錯(cuò)誤,否則可執(zhí)行文件將返回絕對(duì)路徑。
主要用例是找到相對(duì)于可執(zhí)行文件的資源。
nacl 不支持可執(zhí)行文件。
func Exit(code int)
退出時(shí)會(huì)導(dǎo)致當(dāng)前程序退出并顯示給定的狀態(tài)碼。通常,代碼0表示成功,錯(cuò)誤不為零。該程序立即終止; 延遲功能不運(yùn)行。
func Expand(s string, mapping func(string) string) string
根據(jù)映射函數(shù)展開(kāi)取代字符串中的 $ {var} 或 $ var 。例如,os.ExpandEnv(s) 等同于 os.Expand(s,os.Getenv)。
func ExpandEnv(s string) string
ExpandEnv 根據(jù)當(dāng)前環(huán)境變量的值替換字符串中的 $ {var} 或 $ var。未定義變量的引用被空字符串替換。
package mainimport ("fmt""os")func main() { fmt.Println(os.ExpandEnv("$USER lives in ${HOME}."))}
func Getegid() int
Getegid 返回調(diào)用者的數(shù)字有效組 ID 。
在 Windows 上,它返回-1。
func Getenv(key string) string
Getenv 檢索由密鑰命名的環(huán)境變量的值。它返回值,如果該變量不存在,該值將為空。要區(qū)分空值和未設(shè)值,請(qǐng)使用 LookupEnv 。
package mainimport ("fmt""os")func main() { fmt.Printf("%s lives in %s.\n", os.Getenv("USER"), os.Getenv("HOME"))}
func Geteuid() int
Geteuid 返回調(diào)用者的數(shù)字有效用戶(hù)標(biāo)識(shí)。
在 Windows 上,它返回-1。
func Getgid() int
Getgid返回調(diào)用者的數(shù)字組ID。
在Windows上,它返回-1。
func Getgroups() ([]int, error)
Getgroups 返回調(diào)用者所屬組的數(shù)字 ID 列表。
在 Windows 上,它返回 syscall.EWINDOWS 。請(qǐng)參閱 os/user 軟件包以獲取可能的替代方案。
func Getpagesize() int
Getpagesize 返回底層系統(tǒng)的內(nèi)存頁(yè)面大小。
func Getpid() int
Getpid 返回調(diào)用者的進(jìn)程 ID 。
func Getppid() int
Getppid 返回調(diào)用者父進(jìn)程的 ID 。
func Getuid() int
Getuid 返回調(diào)用者的數(shù)字用戶(hù)標(biāo)識(shí)。
在 Windows 上,它返回-1。
func Getwd() (dir string, err error)
Getwd 返回與當(dāng)前目錄對(duì)應(yīng)的根路徑名稱(chēng)。如果當(dāng)前目錄可以通過(guò)多個(gè)路徑到達(dá)(由于符號(hào)鏈接),Getwd 可以返回其中任何一個(gè)。
func Hostname() (name string, err error)
主機(jī)名返回內(nèi)核報(bào)告的主機(jī)名。
func IsExist(err error) bool
IsExist 返回一個(gè)布爾值,指示是否已知錯(cuò)誤報(bào)告文件或目錄已存在。它由 ErrExist 滿(mǎn)足以及一些系統(tǒng)調(diào)用錯(cuò)誤。
func IsNotExist(err error) bool
IsNotExist 返回一個(gè)布爾值,指示是否已知錯(cuò)誤報(bào)告文件或目錄不存在。它由 ErrNotExist 以及一些系統(tǒng)調(diào)用錯(cuò)誤滿(mǎn)足。
package mainimport ("fmt""os")func main() { filename := "a-nonexistent-file"if _, err := os.Stat(filename); os.IsNotExist(err) { fmt.Printf("file does not exist")}}
func IsPathSeparator(c uint8) bool
IsPathSeparator 報(bào)告 c 是否是目錄分隔符。
func IsPermission(err error) bool
IsPermission 返回一個(gè)布爾值,指示是否已知錯(cuò)誤報(bào)告許可被拒絕。它由 ErrPermission 以及一些系統(tǒng)調(diào)用錯(cuò)誤滿(mǎn)足。
func Lchown(name string, uid, gid int) error
Lchown 更改指定文件的數(shù)字 uid 和 gid 。如果該文件是符號(hào)鏈接,它會(huì)更改鏈接本身的 uid 和 gid 。如果有錯(cuò)誤,它將是 * PathError 類(lèi)型。
在 Windows 上,它總是返回包裝在 * PathError 中的 syscall.EWINDOWS 錯(cuò)誤。
func Link(oldname, newname string) error
鏈接創(chuàng)建新名稱(chēng)作為舊名稱(chēng)文件的硬鏈接。如果有錯(cuò)誤,它將是 * LinkError 類(lèi)型。
func LookupEnv(key string) (string, bool)
LookupEnv 檢索由密鑰命名的環(huán)境變量的值。如果變量存在于環(huán)境中,則返回值(可能為空),布爾值為 true 。否則,返回的值將為空,布爾值將為 false 。
package mainimport ("fmt""os")func main() { show := func(key string) { val, ok := os.LookupEnv(key)if !ok { fmt.Printf("%s not set\n", key)} else { fmt.Printf("%s=%s\n", key, val)}}show("USER")show("GOPATH")}
func Mkdir(name string, perm FileMode) error
Mkdir 使用指定的名稱(chēng)和權(quán)限位創(chuàng)建一個(gè)新目錄。如果有錯(cuò)誤,它將是 * PathError 類(lèi)型。
func MkdirAll(path string, perm FileMode) error
MkdirAll 會(huì)創(chuàng)建一個(gè)名為 path 的目錄以及任何必要的父項(xiàng),并返回 nil ,否則返回錯(cuò)誤。許可位 perm 用于 MkdirAll 創(chuàng)建的所有目錄。如果 path 已經(jīng)是一個(gè)目錄,MkdirAll 什么也不做,并返回 nil 。
func NewSyscallError(syscall string, err error) error
NewSyscallError 返回一個(gè)新的 SyscallError 作為錯(cuò)誤,并給出系統(tǒng)調(diào)用名稱(chēng)和錯(cuò)誤詳細(xì)信息。為方便起見(jiàn),如果 err 為零,則 NewSyscallError 返回 nil 。
func Readlink(name string) (string, error)
Readlink 返回指定符號(hào)鏈接的目的地。如果有錯(cuò)誤,它將是 * PathError 類(lèi)型。
func Remove(name string) error
刪除將刪除指定的文件或目錄。如果有錯(cuò)誤,它將是 * PathError 類(lèi)型。
func RemoveAll(path string) error
RemoveAll 移除路徑及其包含的任何子項(xiàng)。它刪除所有可能的東西,但返回遇到的第一個(gè)錯(cuò)誤。如果路徑不存在,RemoveAll 返回 nil(無(wú)錯(cuò)誤)。
func Rename(oldpath, newpath string) error
重命名(移動(dòng))舊路徑到新路徑。如果 newpath 已經(jīng)存在并且不是目錄,則重命名將替換它。當(dāng) oldpath和 newpath 位于不同的目錄中時(shí),可能會(huì)應(yīng)用 OS 特定的限制。如果有錯(cuò)誤,它將是 * LinkError 類(lèi)型。
func SameFile(fi1, fi2 FileInfo) bool
SameFile 報(bào)告 fi1 和 fi2 是否描述相同的文件。例如,在 Unix 上,這意味著兩個(gè)基礎(chǔ)結(jié)構(gòu)的設(shè)備和 inode 字段是相同的; 在其他系統(tǒng)上,決策可以基于路徑名稱(chēng)。 SameFile 僅適用于此包的統(tǒng)計(jì)信息返回的結(jié)果。在其他情況下它返回 false 。
func Setenv(key, value string) error
Setenv 設(shè)置由密鑰命名的環(huán)境變量的值。它返回一個(gè)錯(cuò)誤,如果有的話(huà)。
func Symlink(oldname, newname string) error
符號(hào)鏈接創(chuàng)建新名稱(chēng)作為舊名稱(chēng)的符號(hào)鏈接。如果有錯(cuò)誤,它將是 * LinkError 類(lèi)型。
func TempDir() string
TempDir 返回用于臨時(shí)文件的默認(rèn)目錄。
在 Unix 系統(tǒng)上,如果非空則返回 $TMPDIR,否則返回 /tmp 。在 Windows 上,它使用 GetTempPath ,從%TMP%,%TEMP%,%USERPROFILE%或Windows目錄中返回第一個(gè)非空值。在計(jì)劃9中,它返回/ tmp。
該目錄既不保證存在也不具有可訪問(wèn)的權(quán)限。
func Truncate(name string, size int64) error
截?cái)喔闹付ㄎ募拇笮 H绻募欠?hào)鏈接,它將更改鏈接目標(biāo)的大小。如果有錯(cuò)誤,它將是 * PathError 類(lèi)型。
func Unsetenv(key string) error
Unsetenv 取消單個(gè)環(huán)境變量。
package mainimport ("os")func main() { os.Setenv("TMPDIR", "/my/tmp") defer os.Unsetenv("TMPDIR")}
文件表示一個(gè)打開(kāi)的文件描述符。
type File struct { // contains filtered or unexported fields}
func Create(name string) (*File, error)
Create 使用模式0666(在 umask 之前)創(chuàng)建命名文件,如果它已經(jīng)存在,則截?cái)嗨?。如果成功,返回文件上的方法可用?I/O ; 關(guān)聯(lián)的文件描述符具有模式 O_RDWR 。如果有錯(cuò)誤,它將是 * PathError 類(lèi)型。
func NewFile(fd uintptr, name string) *File
NewFile 使用給定的文件描述符和名稱(chēng)返回一個(gè)新的 File 。如果 fd 不是有效的文件描述符,則返回值為零。
func Open(name string) (*File, error)
打開(kāi)打開(kāi)指定文件以供閱讀。如果成功,返回文件上的方法可用于讀取; 關(guān)聯(lián)的文件描述符具有模式 O_RDONLY 。如果有錯(cuò)誤,它將是 * PathError 類(lèi)型。
func OpenFile(name string, flag int, perm FileMode) (*File, error)
OpenFile 是廣義的公開(kāi)呼叫;大多數(shù)用戶(hù)將使用“打開(kāi)”或“創(chuàng)建”。它打開(kāi)具有指定標(biāo)志(O_RDONLY等)和燙發(fā)(0666等)的指定文件(如果適用)。如果成功,返回文件上的方法可用于 I/O 。如果有錯(cuò)誤,它將是 * PathError 類(lèi)型。
package mainimport ("log""os")func main() { f, err := os.OpenFile("notes.txt", os.O_RDWR|os.O_CREATE, 0755)if err != nil { log.Fatal(err)}if err := f.Close(); err != nil { log.Fatal(err)}}
package mainimport ("log""os")func main() {// If the file doesn't exist, create it, or append to the file f, err := os.OpenFile("access.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)if err != nil { log.Fatal(err)}if _, err := f.Write([]byte("appended some data\n")); err != nil { log.Fatal(err)}if err := f.Close(); err != nil { log.Fatal(err)}}
func Pipe() (r *File, w *File, err error)
管道返回一對(duì)連接的文件; 從寫(xiě)入w的r個(gè)返回字節(jié)中讀取。它返回文件和錯(cuò)誤(如果有的話(huà))。
func (f *File) Chdir() error
Chdir 將當(dāng)前工作目錄更改為文件,該文件必須是目錄。如果有錯(cuò)誤,它將是 * PathError 類(lèi)型。
func (f *File) Chmod(mode FileMode) error
Chmod將文件的模式更改為模式。如果有錯(cuò)誤,它將是* PathError類(lèi)型。
func (f *File) Chown(uid, gid int) error
Chown 更改指定文件的數(shù)字 uid 和 gid 。如果有錯(cuò)誤,它將是 * PathError 類(lèi)型。
在 Windows 上,它總是返回包裝在 * PathError 中的 syscall.EWINDOWS 錯(cuò)誤。
func (f *File) Close() error
關(guān)閉關(guān)閉文件,使其不能用于 I/O 。它返回一個(gè)錯(cuò)誤,如果有的話(huà)。
func (f *File) Fd() uintptr
Fd 返回引用打開(kāi)文件的整數(shù) Unix 文件描述符。文件描述符僅在 f.Close 被調(diào)用或f被垃圾收集之前有效。
func (f *File) Name() string
Name 返回提供給 Open 的文件的名稱(chēng)。
func (f *File) Read(b []byte) (n int, err error)
Read 從文件讀取 len(b) 個(gè)字節(jié)。它返回讀取的字節(jié)數(shù)和遇到的任何錯(cuò)誤。在文件結(jié)尾,Read 返回0, io.EOF 。
func (f *File) ReadAt(b []byte, off int64) (n int, err error)
ReadAt 從文件開(kāi)始以字節(jié)偏移關(guān)閉讀取 len(b) 個(gè)字節(jié)。它返回讀取的字節(jié)數(shù)和錯(cuò)誤(如果有的話(huà))。當(dāng) n <len(b) 時(shí), ReadAt 總是返回非零錯(cuò)誤。在文件結(jié)尾處,該錯(cuò)誤是 io.EOF 。
func (f *File) Readdir(n int) ([]FileInfo, error)
Readdir 讀取與文件關(guān)聯(lián)的目錄的內(nèi)容,并以目錄順序返回最多 n 個(gè) FileInfo 值的片段,Lstat 將返回該片段。隨后對(duì)相同文件的調(diào)用將產(chǎn)生更多的 FileInfos 。
如果 n> 0,Readdir 最多返回 n 個(gè) FileInfo 結(jié)構(gòu)。在這種情況下,如果 Readdir 返回一個(gè)空片段,它將返回一個(gè)非零錯(cuò)誤來(lái)解釋原因。在目錄結(jié)尾處,錯(cuò)誤是 io.EOF 。
如果 n<=0,則 Readdir 從單個(gè)切片中的目錄返回所有 FileInfo 。在這種情況下,如果 Readdir 成功(一直讀取到目錄的末尾),它將返回切片并返回一個(gè)零錯(cuò)誤。如果在目錄結(jié)束之前遇到錯(cuò)誤,則 Readdir 將返回 FileInfo 讀取直到該點(diǎn),并出現(xiàn)非零錯(cuò)誤。
func (f *File) Readdirnames(n int) (names []string, err error)
Readdirnames 讀取并返回目錄f中的一段名稱(chēng)。
如果 n>0,Readdirnames 最多返回 n 個(gè)名稱(chēng)。在這種情況下,如果 Readdirnames 返回一個(gè)空片段,它將返回一個(gè)非零錯(cuò)誤來(lái)解釋原因。在目錄結(jié)尾處,錯(cuò)誤是 io.EOF 。
如果n <= 0,則 Readdirnames 將返回單個(gè)切片中目錄中的所有名稱(chēng)。在這種情況下,如果 Readdirnames 成功(一直讀到目錄的末尾),它將返回切片并返回一個(gè)零錯(cuò)誤。如果在目錄結(jié)束之前遇到錯(cuò)誤,則 Readdirnames 將返回直到該點(diǎn)時(shí)讀取的名稱(chēng)以及非零錯(cuò)誤。
func (f *File) Seek(offset int64, whence int) (ret int64, err error)
Seek 將下一個(gè) Read 或 Write on 文件的偏移量設(shè)置為偏移量,根據(jù)此解釋?zhuān)?表示相對(duì)于文件原點(diǎn),1表示相對(duì)于當(dāng)前偏移量,2表示相對(duì)于結(jié)束。它返回新的偏移量和一個(gè)錯(cuò)誤,如果有的話(huà)。未指定使用 O_APPEND 打開(kāi)的文件上的 Seek 行為。
func (f *File) Stat() (FileInfo, error)
Stat 返回描述文件的 FileInfo 結(jié)構(gòu)。如果有錯(cuò)誤,它將是 * PathError 類(lèi)型。
func (f *File) Sync() error
同步將文件的當(dāng)前內(nèi)容提交到穩(wěn)定存儲(chǔ)。通常,這意味著將文件系統(tǒng)的最近寫(xiě)入數(shù)據(jù)的內(nèi)存副本清除到磁盤(pán)。
func (f *File) Truncate(size int64) error
截?cái)喔奈募拇笮?。它不?huì)更改 I/O 偏移量。如果有錯(cuò)誤,它將是 * PathError 類(lèi)型。
func (f *File) Write(b []byte) (n int, err error)
寫(xiě)入 len(b) 字節(jié)到文件。它返回寫(xiě)入的字節(jié)數(shù)和錯(cuò)誤(如果有的話(huà))。當(dāng) n!= len(b) 時(shí),Write 返回非零錯(cuò)誤。
func (f *File) WriteAt(b []byte, off int64) (n int, err error)
WriteAt 將 len(b) 個(gè)字節(jié)寫(xiě)入從字節(jié)偏移 off 開(kāi)始的 File 。它返回寫(xiě)入的字節(jié)數(shù)和錯(cuò)誤(如果有的話(huà))。當(dāng) n!= len(b) 時(shí),WriteAt 返回一個(gè)非零錯(cuò)誤。
func (f *File) WriteString(s string) (n int, err error)
WriteString 就像 Write 一樣,但是寫(xiě)入字符串s的內(nèi)容而不是一個(gè)字節(jié)片段。
FileInfo 描述一個(gè)文件,并由 Stat 和 Lstat 返回。
type FileInfo interface { Name() string // base name of the file Size() int64 // length in bytes for regular files; system-dependent for others Mode() FileMode // file mode bits ModTime() time.Time // modification time IsDir() bool // abbreviation for Mode().IsDir() Sys() interface{} // underlying data source (can return nil)}
func Lstat(name string) (FileInfo, error)
Lstat 返回一個(gè)描述指定文件的 FileInfo 。如果文件是符號(hào)鏈接,則返回的 FileInfo 描述符號(hào)鏈接。Lstat 不會(huì)嘗試跟隨鏈接。如果有錯(cuò)誤,它將是 * PathError 類(lèi)型。
func Stat(name string) (FileInfo, error)
Stat 返回一個(gè)描述指定文件的 FileInfo 。如果有錯(cuò)誤,它將是 * PathError 類(lèi)型。
FileMode 表示文件的模式和權(quán)限位。這些位在所有系統(tǒng)上具有相同的定義,以便可以將有關(guān)文件的信息從一個(gè)系統(tǒng)移動(dòng)到另一個(gè)系統(tǒng)。并非所有位都適用于所有系統(tǒng)。目錄的唯一必需位是 ModeDir 。
type FileMode uint32
定義的文件模式位是 FileMode 的最高有效位。九個(gè)最低有效位是標(biāo)準(zhǔn)的 Unix rwxrwxrwx 權(quán)限。這些位的值應(yīng)被視為公共 API 的一部分,并且可以用于有線(xiàn)協(xié)議或磁盤(pán)表示:盡管可能會(huì)添加新位,但不得更改它們。
const ( // The single letters are the abbreviations // used by the String method's formatting. ModeDir FileMode = 1 << (32 - 1 - iota) // d: is a directory ModeAppend // a: append-only ModeExclusive // l: exclusive use ModeTemporary // T: temporary file; Plan 9 only ModeSymlink // L: symbolic link ModeDevice // D: device file ModeNamedPipe // p: named pipe (FIFO) ModeSocket // S: Unix domain socket ModeSetuid // u: setuid ModeSetgid // g: setgid ModeCharDevice // c: Unix character device, when ModeDevice is set ModeSticky // t: sticky // Mask for the type bits. For regular files, none will be set. ModeType = ModeDir | ModeSymlink | ModeNamedPipe | ModeSocket | ModeDevice ModePerm FileMode = 0777 // Unix permission bits)
package mainimport ("fmt""log""os")func main() { fi, err := os.Lstat("some-filename")if err != nil { log.Fatal(err)}switch mode := fi.Mode(); {case mode.IsRegular(): fmt.Println("regular file")case mode.IsDir(): fmt.Println("directory")case mode&os.ModeSymlink != 0: fmt.Println("symbolic link")case mode&os.ModeNamedPipe != 0: fmt.Println("named pipe")}}
func (m FileMode) IsDir() bool
IsDir 報(bào)告 m 是否描述目錄。也就是說(shuō),它測(cè)試 ModeDir 位在 m 中設(shè)置。
func (m FileMode) IsRegular() bool
IsRegular 報(bào)告 m 是否描述常規(guī)文件。也就是說(shuō),它測(cè)試沒(méi)有設(shè)置模式類(lèi)型位。
func (m FileMode) Perm() FileMode
Perm 以 m 為單位返回 Unix 權(quán)限位。
func (m FileMode) String() string
LinkError 在鏈接或符號(hào)鏈接期間記錄錯(cuò)誤或重命名系統(tǒng)調(diào)用以及導(dǎo)致它的路徑。
type LinkError struct { Op string Old string New string Err error}
func (e *LinkError) Error() string
PathError 記錄錯(cuò)誤以及導(dǎo)致它的操作和文件路徑。
type PathError struct { Op string Path string Err error}
func (e *PathError) Error() string
ProcAttr 保存將應(yīng)用于由 StartProcess 啟動(dòng)的新進(jìn)程的屬性。
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}
進(jìn)程存儲(chǔ)有關(guān)由 StartProcess 創(chuàng)建的進(jìn)程的信息。
type Process struct { Pid int // contains filtered or unexported fields}
func FindProcess(pid int) (*Process, error)
FindProcess 通過(guò)它的 pid 查找正在運(yùn)行的進(jìn)程。
它返回的進(jìn)程可用于獲取有關(guān)底層操作系統(tǒng)進(jìn)程的信息。
在 Unix 系統(tǒng)上,無(wú)論過(guò)程是否存在,F(xiàn)indProcess 都會(huì)成功并為給定的 PID 返回一個(gè) Process 。
func StartProcess(name string, argv []string, attr *ProcAttr) (*Process, error)
StartProcess 使用由 name ,argv 和 attr 指定的程序,參數(shù)和屬性啟動(dòng)一個(gè)新進(jìn)程。
StartProcess 是一個(gè)低級(jí)別的界面。os/exec 軟件包提供更高級(jí)的接口。
如果有錯(cuò)誤,它將是 * PathError 類(lèi)型。
func (p *Process) Kill() error
殺死導(dǎo)致進(jìn)程立即退出。
func (p *Process) Release() error
釋放將釋放與進(jìn)程 p 關(guān)聯(lián)的任何資源,以便將來(lái)無(wú)法使用。只有等待時(shí)才需要調(diào)用 Release 。
func (p *Process) Signal(sig Signal) error
信號(hào)向過(guò)程發(fā)送信號(hào)。未在Windows上發(fā)送中斷。
func (p *Process) Wait() (*ProcessState, error)
等待進(jìn)程退出,然后返回描述其狀態(tài)和錯(cuò)誤(如果有的話(huà))的 ProcessState 。等待釋放與流程相關(guān)的任何資源。在大多數(shù)操作系統(tǒng)上,進(jìn)程必須是當(dāng)前進(jìn)程的子進(jìn)程,否則將返回錯(cuò)誤。
ProcessState 存儲(chǔ)有關(guān)由 Wait 報(bào)告的進(jìn)程的信息。
type ProcessState struct { // contains filtered or unexported fields}
func (p *ProcessState) Exited() bool
已退出的報(bào)告是否已退出該程序。
func (p *ProcessState) Pid() int
Pid 返回退出進(jìn)程的進(jìn)程 ID 。
func (p *ProcessState) String() string
func (p *ProcessState) Success() bool
成功報(bào)告程序是否成功退出,例如 Unix 上的退出狀態(tài)為0。
func (p *ProcessState) Sys() interface{}
Sys 返回有關(guān)該過(guò)程的系統(tǒng)相關(guān)退出信息。將其轉(zhuǎn)換為適當(dāng)?shù)幕A(chǔ)類(lèi)型,例如 Unix 上的 syscall.WaitStatus 以訪問(wèn)其內(nèi)容。
func (p *ProcessState) SysUsage() interface{}
SysUsage 返回有關(guān)退出進(jìn)程的系統(tǒng)相關(guān)資源使用信息。將其轉(zhuǎn)換為適當(dāng)?shù)幕A(chǔ)類(lèi)型,例如 Unix 上的 * syscall.Rusage 以訪問(wèn)其內(nèi)容。(在 Unix 上,* syscall.Rusage與getrusage(2)手冊(cè)頁(yè)中定義的 struct rusage 匹配。)
func (p *ProcessState) SystemTime() time.Duration
SystemTime 返回退出進(jìn)程及其子進(jìn)程的系統(tǒng) CPU 時(shí)間。
func (p *ProcessState) UserTime() time.Duration
UserTime 返回退出進(jìn)程及其子進(jìn)程的用戶(hù) CPU 時(shí)間。
信號(hào)表示操作系統(tǒng)信號(hào)。通常的底層實(shí)現(xiàn)是操作系統(tǒng)相關(guān)的:在 Unix 上是 syscall.Signal 。
type Signal interface { String() string Signal() // to distinguish from other Stringers}
唯一保證在所有系統(tǒng)上存在的信號(hào)值是中斷(發(fā)送進(jìn)程中斷)和終止(強(qiáng)制進(jìn)程退出)。
var ( Interrupt Signal = syscall.SIGINT Kill Signal = syscall.SIGKILL)
SyscallError 記錄來(lái)自特定系統(tǒng)調(diào)用的錯(cuò)誤。
type SyscallError struct { Syscall string Err error}
func (e *SyscallError) Error() string