亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

directory search
archive archive/tar archive/zip bufio bufio(緩存) builtin builtin(內(nèi)置包) bytes bytes(包字節(jié)) compress compress/bzip2(壓縮/bzip2) compress/flate(壓縮/flate) compress/gzip(壓縮/gzip) compress/lzw(壓縮/lzw) compress/zlib(壓縮/zlib) container container/heap(容器數(shù)據(jù)結(jié)構heap) container/list(容器數(shù)據(jù)結(jié)構list) container/ring(容器數(shù)據(jù)結(jié)構ring) context context(上下文) crypto crypto(加密) crypto/aes(加密/aes) crypto/cipher(加密/cipher) crypto/des(加密/des) crypto/dsa(加密/dsa) crypto/ecdsa(加密/ecdsa) crypto/elliptic(加密/elliptic) crypto/hmac(加密/hmac) crypto/md5(加密/md5) crypto/rand(加密/rand) crypto/rc4(加密/rc4) crypto/rsa(加密/rsa) crypto/sha1(加密/sha1) crypto/sha256(加密/sha256) crypto/sha512(加密/sha512) crypto/subtle(加密/subtle) crypto/tls(加密/tls) crypto/x509(加密/x509) crypto/x509/pkix(加密/x509/pkix) database database/sql(數(shù)據(jù)庫/sql) database/sql/driver(數(shù)據(jù)庫/sql/driver) debug debug/dwarf(調(diào)試/dwarf) debug/elf(調(diào)試/elf) debug/gosym(調(diào)試/gosym) debug/macho(調(diào)試/macho) debug/pe(調(diào)試/pe) debug/plan9obj(調(diào)試/plan9obj) encoding encoding(編碼) encoding/ascii85(編碼/ascii85) encoding/asn1(編碼/asn1) encoding/base32(編碼/base32) encoding/base64(編碼/base64) encoding/binary(編碼/binary) encoding/csv(編碼/csv) encoding/gob(編碼/gob) encoding/hex(編碼/hex) encoding/json(編碼/json) encoding/pem(編碼/pem) encoding/xml(編碼/xml) errors errors(錯誤) expvar expvar flag flag(命令行參數(shù)解析flag包) fmt fmt go go/ast(抽象語法樹) go/build go/constant(常量) go/doc(文檔) go/format(格式) go/importer go/parser go/printer go/scanner(掃描儀) go/token(令牌) go/types(類型) hash hash(散列) hash/adler32 hash/crc32 hash/crc64 hash/fnv html html html/template(模板) image image(圖像) image/color(顏色) image/color/palette(調(diào)色板) image/draw(繪圖) image/gif image/jpeg image/png index index/suffixarray io io io/ioutil log log log/syslog(日志系統(tǒng)) math math math/big math/big math/bits math/bits math/cmplx math/cmplx math/rand math/rand mime mime mime/multipart(多部分) mime/quotedprintable net net net/http net/http net/http/cgi net/http/cookiejar net/http/fcgi net/http/httptest net/http/httptrace net/http/httputil net/http/internal net/http/pprof net/mail net/mail net/rpc net/rpc net/rpc/jsonrpc net/smtp net/smtp net/textproto net/textproto net/url net/url os os os/exec os/signal os/user path path path/filepath(文件路徑) plugin plugin(插件) reflect reflect(反射) regexp regexp(正則表達式) regexp/syntax runtime runtime(運行時) runtime/debug(調(diào)試) runtime/internal/sys runtime/pprof runtime/race(競爭) runtime/trace(執(zhí)行追蹤器) sort sort(排序算法) strconv strconv(轉(zhuǎn)換) strings strings(字符串) sync sync(同步) sync/atomic(原子操作) syscall syscall(系統(tǒng)調(diào)用) testing testing(測試) testing/iotest testing/quick text text/scanner(掃描文本) text/tabwriter text/template(定義模板) text/template/parse time time(時間戳) unicode unicode unicode/utf16 unicode/utf8 unsafe unsafe
characters

  • import "os"

  • Overview

  • Index

  • Examples

  • Subdirectories

概觀

Package os為操作系統(tǒng)功能提供了一個平臺無關的接口。雖然錯誤處理類似于 Go,但設計類似 Unix,失敗的調(diào)用返回類型錯誤的值而不是錯誤號。錯誤中通常會提供更多信息。例如,如果接收文件名的調(diào)用失敗(例如 Open 或 Stat ),則錯誤將在打印時包含失敗的文件名,并且將是 * PathError 類型,可能會解壓縮以獲取更多信息。

OS 界面旨在使所有操作系統(tǒng)均勻。通常不可用的功能出現(xiàn)在系統(tǒng)特定的軟件包 syscall 中。

這里有一個簡單的例子,打開一個文件并閱讀一些文件。

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

如果打開失敗,錯誤字符串將不言自明,如

open file.go: no such file or directory

然后可以將文件的數(shù)據(jù)讀入一段字節(jié)。讀取和寫入從參數(shù)切片的長度獲取其字節(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])

Index

  • 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

常量

標記為 OpenFile 封裝底層系統(tǒng)的標記。并非所有的標志都可以在給定的系統(tǒng)上實現(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.)

尋求哪些價值。

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)“空設備”的名稱。在類 Unix 系統(tǒng)上,它是“/ dev / null”; 在Windows上,“NUL”。

const DevNull = "/dev/null"

變量

一些常見系統(tǒng)調(diào)用錯誤的便攜式模擬器。

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 是打開的文件,指向標準輸入,標準輸出和標準錯誤文件描述符。

請注意,Go 運行時寫入標準錯誤以防恐慌和崩潰;關閉 Stderr 可能會導致這些消息到其他地方,也許會導致稍后打開的文件。

var (
        Stdin  = NewFile(uintptr(syscall.Stdin), "/dev/stdin")
        Stdout = NewFile(uintptr(syscall.Stdout), "/dev/stdout")
        Stderr = NewFile(uintptr(syscall.Stderr), "/dev/stderr"))

參數(shù)從程序名稱開始保存命令行參數(shù)。

var Args []string

func Chdir

func Chdir(dir string) error

Chdir 將當前工作目錄更改為指定的目錄。如果有錯誤,它將是 * PathError 類型。

func Chmod

func Chmod(name string, mode FileMode) error

Chmod 將指定文件的模式更改為模式。如果文件是符號鏈接,它將更改鏈接目標的模式。如果有錯誤,它將是 * PathError 類型。

取決于操作系統(tǒng),使用模式位的不同子集。

在 Unix 上,使用模式的權限位 ModeSetuid,ModeSetgid 和 ModeSticky 。

在 Windows 上,模式必須是非零的,否則只能使用0200位(所有者可寫)模式; 它控制文件的只讀屬性是設置還是清除。屬性。其他位當前未使用。對于只讀文件使用模式0400,對于可讀+可寫文件使用0600。

在計劃9中,使用模式的許可位 ModeAppend,ModeExclusive 和 ModeTemporary 。

package mainimport ("log""os")func main() {if err := os.Chmod("some-filename", 0644); err != nil {
		log.Fatal(err)}}

func Chown

func Chown(name string, uid, gid int) error

Chown 更改指定文件的數(shù)字 uid 和 gid 。如果該文件是符號鏈接,它會更改鏈接目標的 uid 和 gid 。如果有錯誤,它將是 * PathError 類型。

在 Windows 上,它總是返回包裝在 * PathError 中的 syscall.EWINDOWS 錯誤。

func Chtimes

func Chtimes(name string, atime time.Time, mtime time.Time) error

Chtimes 更改指定文件的訪問和修改時間,類似于 Unix utime() 或 utimes() 函數(shù)。

底層文件系統(tǒng)可能會將值截斷或舍入到不太精確的時間單位。如果有錯誤,它將是 * PathError 類型。

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

func Clearenv()

Clearenv 刪除所有環(huán)境變量。

func Environ

func Environ() []string

Environ 以 “key = value” 的形式返回表示環(huán)境的字符串的副本。

func Executable

func Executable() (string, error)

可執(zhí)行文件返回啟動當前進程的可執(zhí)行文件的路徑名稱。不能保證路徑仍然指向正確的可執(zhí)行文件。如果使用符號鏈接來啟動進程,則根據(jù)操作系統(tǒng)的不同,結(jié)果可能是符號鏈接或它指向的路徑。如果需要穩(wěn)定的結(jié)果, path / filepath.EvalSymlinks 可能會有所幫助。

除非發(fā)生錯誤,否則可執(zhí)行文件將返回絕對路徑。

主要用例是找到相對于可執(zhí)行文件的資源。

nacl 不支持可執(zhí)行文件。

func Exit

func Exit(code int)

退出時會導致當前程序退出并顯示給定的狀態(tài)碼。通常,代碼0表示成功,錯誤不為零。該程序立即終止; 延遲功能不運行。

func Expand

func Expand(s string, mapping func(string) string) string

根據(jù)映射函數(shù)展開取代字符串中的 $ {var} 或 $ var 。例如,os.ExpandEnv(s) 等同于 os.Expand(s,os.Getenv)。

func ExpandEnv

func ExpandEnv(s string) string

ExpandEnv 根據(jù)當前環(huán)境變量的值替換字符串中的 $ {var} 或 $ var。未定義變量的引用被空字符串替換。

package mainimport ("fmt""os")func main() {
	fmt.Println(os.ExpandEnv("$USER lives in ${HOME}."))}

func Getegid

func Getegid() int

Getegid 返回調(diào)用者的數(shù)字有效組 ID 。

在 Windows 上,它返回-1。

func Getenv

func Getenv(key string) string

Getenv 檢索由密鑰命名的環(huán)境變量的值。它返回值,如果該變量不存在,該值將為空。要區(qū)分空值和未設值,請使用 LookupEnv 。

package mainimport ("fmt""os")func main() {
	fmt.Printf("%s lives in %s.\n", os.Getenv("USER"), os.Getenv("HOME"))}

func Geteuid

func Geteuid() int

Geteuid 返回調(diào)用者的數(shù)字有效用戶標識。

在 Windows 上,它返回-1。

func Getgid

func Getgid() int

Getgid返回調(diào)用者的數(shù)字組ID。

在Windows上,它返回-1。

func Getgroups

func Getgroups() ([]int, error)

Getgroups 返回調(diào)用者所屬組的數(shù)字 ID 列表。

在 Windows 上,它返回 syscall.EWINDOWS 。請參閱 os/user 軟件包以獲取可能的替代方案。

func Getpagesize

func Getpagesize() int

Getpagesize 返回底層系統(tǒng)的內(nèi)存頁面大小。

func Getpid

func Getpid() int

Getpid 返回調(diào)用者的進程 ID 。

func Getppid

func Getppid() int

Getppid 返回調(diào)用者父進程的 ID 。

func Getuid

func Getuid() int

Getuid 返回調(diào)用者的數(shù)字用戶標識。

在 Windows 上,它返回-1。

func Getwd

func Getwd() (dir string, err error)

Getwd 返回與當前目錄對應的根路徑名稱。如果當前目錄可以通過多個路徑到達(由于符號鏈接),Getwd 可以返回其中任何一個。

func Hostname

func Hostname() (name string, err error)

主機名返回內(nèi)核報告的主機名。

func IsExist

func IsExist(err error) bool

IsExist 返回一個布爾值,指示是否已知錯誤報告文件或目錄已存在。它由 ErrExist 滿足以及一些系統(tǒng)調(diào)用錯誤。

func IsNotExist

func IsNotExist(err error) bool

IsNotExist 返回一個布爾值,指示是否已知錯誤報告文件或目錄不存在。它由 ErrNotExist 以及一些系統(tǒng)調(diào)用錯誤滿足。

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

func IsPathSeparator(c uint8) bool

IsPathSeparator 報告 c 是否是目錄分隔符。

func IsPermission

func IsPermission(err error) bool

IsPermission 返回一個布爾值,指示是否已知錯誤報告許可被拒絕。它由 ErrPermission 以及一些系統(tǒng)調(diào)用錯誤滿足。

func Lchown

func Lchown(name string, uid, gid int) error

Lchown 更改指定文件的數(shù)字 uid 和 gid 。如果該文件是符號鏈接,它會更改鏈接本身的 uid 和 gid 。如果有錯誤,它將是 * PathError 類型。

在 Windows 上,它總是返回包裝在 * PathError 中的 syscall.EWINDOWS 錯誤。

func Link

func Link(oldname, newname string) error

鏈接創(chuàng)建新名稱作為舊名稱文件的硬鏈接。如果有錯誤,它將是 * LinkError 類型。

func LookupEnv

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

func Mkdir(name string, perm FileMode) error

Mkdir 使用指定的名稱和權限位創(chuàng)建一個新目錄。如果有錯誤,它將是 * PathError 類型。

func MkdirAll

func MkdirAll(path string, perm FileMode) error

MkdirAll 會創(chuàng)建一個名為 path 的目錄以及任何必要的父項,并返回 nil ,否則返回錯誤。許可位 perm 用于 MkdirAll 創(chuàng)建的所有目錄。如果 path 已經(jīng)是一個目錄,MkdirAll 什么也不做,并返回 nil 。

func NewSyscallError

func NewSyscallError(syscall string, err error) error

NewSyscallError 返回一個新的 SyscallError 作為錯誤,并給出系統(tǒng)調(diào)用名稱和錯誤詳細信息。為方便起見,如果 err 為零,則 NewSyscallError 返回 nil 。

func Readlink

func Readlink(name string) (string, error)

Readlink 返回指定符號鏈接的目的地。如果有錯誤,它將是 * PathError 類型。

func Remove

func Remove(name string) error

刪除將刪除指定的文件或目錄。如果有錯誤,它將是 * PathError 類型。

func RemoveAll

func RemoveAll(path string) error

RemoveAll 移除路徑及其包含的任何子項。它刪除所有可能的東西,但返回遇到的第一個錯誤。如果路徑不存在,RemoveAll 返回 nil(無錯誤)。

func Rename

func Rename(oldpath, newpath string) error

重命名(移動)舊路徑到新路徑。如果 newpath 已經(jīng)存在并且不是目錄,則重命名將替換它。當 oldpath和 newpath 位于不同的目錄中時,可能會應用 OS 特定的限制。如果有錯誤,它將是 * LinkError 類型。

func SameFile

func SameFile(fi1, fi2 FileInfo) bool

SameFile 報告 fi1 和 fi2 是否描述相同的文件。例如,在 Unix 上,這意味著兩個基礎結(jié)構的設備和 inode 字段是相同的; 在其他系統(tǒng)上,決策可以基于路徑名稱。 SameFile 僅適用于此包的統(tǒng)計信息返回的結(jié)果。在其他情況下它返回 false 。

func Setenv

func Setenv(key, value string) error

Setenv 設置由密鑰命名的環(huán)境變量的值。它返回一個錯誤,如果有的話。

func Symlink

func Symlink(oldname, newname string) error

符號鏈接創(chuàng)建新名稱作為舊名稱的符號鏈接。如果有錯誤,它將是 * LinkError 類型。

func TempDir

func TempDir() string

TempDir 返回用于臨時文件的默認目錄。

在 Unix 系統(tǒng)上,如果非空則返回 $TMPDIR,否則返回 /tmp 。在 Windows 上,它使用 GetTempPath ,從%TMP%,%TEMP%,%USERPROFILE%或Windows目錄中返回第一個非空值。在計劃9中,它返回/ tmp。

該目錄既不保證存在也不具有可訪問的權限。

func Truncate

func Truncate(name string, size int64) error

截斷更改指定文件的大小。如果文件是符號鏈接,它將更改鏈接目標的大小。如果有錯誤,它將是 * PathError 類型。

func Unsetenv

func Unsetenv(key string) error

Unsetenv 取消單個環(huán)境變量。

package mainimport ("os")func main() {
	os.Setenv("TMPDIR", "/my/tmp")
	defer os.Unsetenv("TMPDIR")}

type File

文件表示一個打開的文件描述符。

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

func Create

func Create(name string) (*File, error)

Create 使用模式0666(在 umask 之前)創(chuàng)建命名文件,如果它已經(jīng)存在,則截斷它。如果成功,返回文件上的方法可用于 I/O ; 關聯(lián)的文件描述符具有模式 O_RDWR 。如果有錯誤,它將是 * PathError 類型。

func NewFile

func NewFile(fd uintptr, name string) *File

NewFile 使用給定的文件描述符和名稱返回一個新的 File 。如果 fd 不是有效的文件描述符,則返回值為零。

func Open

func Open(name string) (*File, error)

打開打開指定文件以供閱讀。如果成功,返回文件上的方法可用于讀取; 關聯(lián)的文件描述符具有模式 O_RDONLY 。如果有錯誤,它將是 * PathError 類型。

func OpenFile

func OpenFile(name string, flag int, perm FileMode) (*File, error)

OpenFile 是廣義的公開呼叫;大多數(shù)用戶將使用“打開”或“創(chuàng)建”。它打開具有指定標志(O_RDONLY等)和燙發(fā)(0666等)的指定文件(如果適用)。如果成功,返回文件上的方法可用于 I/O 。如果有錯誤,它將是 * PathError 類型。

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

func Pipe() (r *File, w *File, err error)

管道返回一對連接的文件; 從寫入w的r個返回字節(jié)中讀取。它返回文件和錯誤(如果有的話)。

func (*File) Chdir

func (f *File) Chdir() error

Chdir 將當前工作目錄更改為文件,該文件必須是目錄。如果有錯誤,它將是 * PathError 類型。

func (*File) Chmod

func (f *File) Chmod(mode FileMode) error

Chmod將文件的模式更改為模式。如果有錯誤,它將是* PathError類型。

func (*File) Chown

func (f *File) Chown(uid, gid int) error

Chown 更改指定文件的數(shù)字 uid 和 gid 。如果有錯誤,它將是 * PathError 類型。

在 Windows 上,它總是返回包裝在 * PathError 中的 syscall.EWINDOWS 錯誤。

func (*File) Close

func (f *File) Close() error

關閉關閉文件,使其不能用于 I/O 。它返回一個錯誤,如果有的話。

func (*File) Fd

func (f *File) Fd() uintptr

Fd 返回引用打開文件的整數(shù) Unix 文件描述符。文件描述符僅在 f.Close 被調(diào)用或f被垃圾收集之前有效。

func (*File) Name

func (f *File) Name() string

Name 返回提供給 Open 的文件的名稱。

func (*File) Read

func (f *File) Read(b []byte) (n int, err error)

Read 從文件讀取 len(b) 個字節(jié)。它返回讀取的字節(jié)數(shù)和遇到的任何錯誤。在文件結(jié)尾,Read 返回0, io.EOF 。

func (*File) ReadAt

func (f *File) ReadAt(b []byte, off int64) (n int, err error)

ReadAt 從文件開始以字節(jié)偏移關閉讀取 len(b) 個字節(jié)。它返回讀取的字節(jié)數(shù)和錯誤(如果有的話)。當 n <len(b) 時, ReadAt 總是返回非零錯誤。在文件結(jié)尾處,該錯誤是 io.EOF 。

func (*File) Readdir

func (f *File) Readdir(n int) ([]FileInfo, error)

Readdir 讀取與文件關聯(lián)的目錄的內(nèi)容,并以目錄順序返回最多 n 個 FileInfo 值的片段,Lstat 將返回該片段。隨后對相同文件的調(diào)用將產(chǎn)生更多的 FileInfos 。

如果 n> 0,Readdir 最多返回 n 個 FileInfo 結(jié)構。在這種情況下,如果 Readdir 返回一個空片段,它將返回一個非零錯誤來解釋原因。在目錄結(jié)尾處,錯誤是 io.EOF 。

如果 n<=0,則 Readdir 從單個切片中的目錄返回所有 FileInfo 。在這種情況下,如果 Readdir 成功(一直讀取到目錄的末尾),它將返回切片并返回一個零錯誤。如果在目錄結(jié)束之前遇到錯誤,則 Readdir 將返回 FileInfo 讀取直到該點,并出現(xiàn)非零錯誤。

func (*File) Readdirnames

func (f *File) Readdirnames(n int) (names []string, err error)

Readdirnames 讀取并返回目錄f中的一段名稱。

如果 n>0,Readdirnames 最多返回 n 個名稱。在這種情況下,如果 Readdirnames 返回一個空片段,它將返回一個非零錯誤來解釋原因。在目錄結(jié)尾處,錯誤是 io.EOF 。

如果n <= 0,則 Readdirnames 將返回單個切片中目錄中的所有名稱。在這種情況下,如果 Readdirnames 成功(一直讀到目錄的末尾),它將返回切片并返回一個零錯誤。如果在目錄結(jié)束之前遇到錯誤,則 Readdirnames 將返回直到該點時讀取的名稱以及非零錯誤。

func (*File) Seek

func (f *File) Seek(offset int64, whence int) (ret int64, err error)

Seek 將下一個 Read 或 Write on 文件的偏移量設置為偏移量,根據(jù)此解釋:0表示相對于文件原點,1表示相對于當前偏移量,2表示相對于結(jié)束。它返回新的偏移量和一個錯誤,如果有的話。未指定使用 O_APPEND 打開的文件上的 Seek 行為。

func (*File) Stat

func (f *File) Stat() (FileInfo, error)

Stat 返回描述文件的 FileInfo 結(jié)構。如果有錯誤,它將是 * PathError 類型。

func (*File) Sync

func (f *File) Sync() error

同步將文件的當前內(nèi)容提交到穩(wěn)定存儲。通常,這意味著將文件系統(tǒng)的最近寫入數(shù)據(jù)的內(nèi)存副本清除到磁盤。

func (*File) Truncate

func (f *File) Truncate(size int64) error

截斷更改文件的大小。它不會更改 I/O 偏移量。如果有錯誤,它將是 * PathError 類型。

func (*File) Write

func (f *File) Write(b []byte) (n int, err error)

寫入 len(b) 字節(jié)到文件。它返回寫入的字節(jié)數(shù)和錯誤(如果有的話)。當 n!= len(b) 時,Write 返回非零錯誤。

func (*File) WriteAt

func (f *File) WriteAt(b []byte, off int64) (n int, err error)

WriteAt 將 len(b) 個字節(jié)寫入從字節(jié)偏移 off 開始的 File 。它返回寫入的字節(jié)數(shù)和錯誤(如果有的話)。當 n!= len(b) 時,WriteAt 返回一個非零錯誤。

func (*File) WriteString

func (f *File) WriteString(s string) (n int, err error)

WriteString 就像 Write 一樣,但是寫入字符串s的內(nèi)容而不是一個字節(jié)片段。

type FileInfo

FileInfo 描述一個文件,并由 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

func Lstat(name string) (FileInfo, error)

Lstat 返回一個描述指定文件的 FileInfo 。如果文件是符號鏈接,則返回的 FileInfo 描述符號鏈接。Lstat 不會嘗試跟隨鏈接。如果有錯誤,它將是 * PathError 類型。

func Stat

func Stat(name string) (FileInfo, error)

Stat 返回一個描述指定文件的 FileInfo 。如果有錯誤,它將是 * PathError 類型。

type FileMode

FileMode 表示文件的模式和權限位。這些位在所有系統(tǒng)上具有相同的定義,以便可以將有關文件的信息從一個系統(tǒng)移動到另一個系統(tǒng)。并非所有位都適用于所有系統(tǒng)。目錄的唯一必需位是 ModeDir 。

type FileMode uint32

定義的文件模式位是 FileMode 的最高有效位。九個最低有效位是標準的 Unix rwxrwxrwx 權限。這些位的值應被視為公共 API 的一部分,并且可以用于有線協(xié)議或磁盤表示:盡管可能會添加新位,但不得更改它們。

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 (FileMode) IsDir

func (m FileMode) IsDir() bool

IsDir 報告 m 是否描述目錄。也就是說,它測試 ModeDir 位在 m 中設置。

func (FileMode) IsRegular

func (m FileMode) IsRegular() bool

IsRegular 報告 m 是否描述常規(guī)文件。也就是說,它測試沒有設置模式類型位。

func (FileMode) Perm

func (m FileMode) Perm() FileMode

Perm 以 m 為單位返回 Unix 權限位。

func (FileMode) String

func (m FileMode) String() string

type LinkError

LinkError 在鏈接或符號鏈接期間記錄錯誤或重命名系統(tǒng)調(diào)用以及導致它的路徑。

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

func (*LinkError) Error

func (e *LinkError) Error() string

type PathError

PathError 記錄錯誤以及導致它的操作和文件路徑。

type PathError struct {
        Op   string
        Path string
        Err  error}

func (*PathError) Error

func (e *PathError) Error() string

type ProcAttr

ProcAttr 保存將應用于由 StartProcess 啟動的新進程的屬性。

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}

type Process

進程存儲有關由 StartProcess 創(chuàng)建的進程的信息。

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

func FindProcess

func FindProcess(pid int) (*Process, error)

FindProcess 通過它的 pid 查找正在運行的進程。

它返回的進程可用于獲取有關底層操作系統(tǒng)進程的信息。

在 Unix 系統(tǒng)上,無論過程是否存在,F(xiàn)indProcess 都會成功并為給定的 PID 返回一個 Process 。

func StartProcess

func StartProcess(name string, argv []string, attr *ProcAttr) (*Process, error)

StartProcess 使用由 name ,argv 和 attr 指定的程序,參數(shù)和屬性啟動一個新進程。

StartProcess 是一個低級別的界面。os/exec 軟件包提供更高級的接口。

如果有錯誤,它將是 * PathError 類型。

func (*Process) Kill

func (p *Process) Kill() error

殺死導致進程立即退出。

func (*Process) Release

func (p *Process) Release() error

釋放將釋放與進程 p 關聯(lián)的任何資源,以便將來無法使用。只有等待時才需要調(diào)用 Release 。

func (*Process) Signal

func (p *Process) Signal(sig Signal) error

信號向過程發(fā)送信號。未在Windows上發(fā)送中斷。

func (*Process) Wait

func (p *Process) Wait() (*ProcessState, error)

等待進程退出,然后返回描述其狀態(tài)和錯誤(如果有的話)的 ProcessState 。等待釋放與流程相關的任何資源。在大多數(shù)操作系統(tǒng)上,進程必須是當前進程的子進程,否則將返回錯誤。

type ProcessState

ProcessState 存儲有關由 Wait 報告的進程的信息。

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

func (*ProcessState) Exited

func (p *ProcessState) Exited() bool

已退出的報告是否已退出該程序。

func (*ProcessState) Pid

func (p *ProcessState) Pid() int

Pid 返回退出進程的進程 ID 。

func (*ProcessState) String

func (p *ProcessState) String() string

func (*ProcessState) Success

func (p *ProcessState) Success() bool

成功報告程序是否成功退出,例如 Unix 上的退出狀態(tài)為0。

func (*ProcessState) Sys

func (p *ProcessState) Sys() interface{}

Sys 返回有關該過程的系統(tǒng)相關退出信息。將其轉(zhuǎn)換為適當?shù)幕A類型,例如 Unix 上的 syscall.WaitStatus 以訪問其內(nèi)容。

func (*ProcessState) SysUsage

func (p *ProcessState) SysUsage() interface{}

SysUsage 返回有關退出進程的系統(tǒng)相關資源使用信息。將其轉(zhuǎn)換為適當?shù)幕A類型,例如 Unix 上的 * syscall.Rusage 以訪問其內(nèi)容。(在 Unix 上,* syscall.Rusage與getrusage(2)手冊頁中定義的 struct rusage 匹配。)

func (*ProcessState) SystemTime

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

SystemTime 返回退出進程及其子進程的系統(tǒng) CPU 時間。

func (*ProcessState) UserTime

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

UserTime 返回退出進程及其子進程的用戶 CPU 時間。

type Signal

信號表示操作系統(tǒng)信號。通常的底層實現(xiàn)是操作系統(tǒng)相關的:在 Unix 上是 syscall.Signal 。

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

唯一保證在所有系統(tǒng)上存在的信號值是中斷(發(fā)送進程中斷)和終止(強制進程退出)。

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

type SyscallError

SyscallError 記錄來自特定系統(tǒng)調(diào)用的錯誤。

type SyscallError struct {
        Syscall string
        Err     error}

func (*SyscallError) Error

func (e *SyscallError) Error() string
Previous article: Next article: