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

目錄 搜索
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é)構(gòu)heap) container/list(容器數(shù)據(jù)結(jié)構(gòu)list) container/ring(容器數(shù)據(jù)結(jié)構(gòu)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ù)庫(kù)/sql) database/sql/driver(數(shù)據(jù)庫(kù)/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(錯(cuò)誤) expvar expvar flag flag(命令行參數(shù)解析flag包) fmt fmt go go/ast(抽象語(yǔ)法樹) 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(正則表達(dá)式) regexp/syntax runtime runtime(運(yùn)行時(shí)) runtime/debug(調(diào)試) runtime/internal/sys runtime/pprof runtime/race(競(jìng)爭(zhēng)) runtime/trace(執(zhí)行追蹤器) sort sort(排序算法) strconv strconv(轉(zhuǎn)換) strings strings(字符串) sync sync(同步) sync/atomic(原子操作) syscall syscall(系統(tǒng)調(diào)用) testing testing(測(cè)試) testing/iotest testing/quick text text/scanner(掃描文本) text/tabwriter text/template(定義模板) text/template/parse time time(時(shí)間戳) unicode unicode unicode/utf16 unicode/utf8 unsafe unsafe
文字

  • import "os"

  • Overview

  • Index

  • Examples

  • Subdirectories

概觀

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

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

這里有一個(gè)簡(jiǎn)單的例子,打開一個(gè)文件并閱讀一些文件。

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

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

open file.go: no such file or directory

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

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

常量

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

const DevNull = "/dev/null"

變量

一些常見系統(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 是打開的文件,指向標(biāo)準(zhǔn)輸入,標(biāo)準(zhǔn)輸出和標(biāo)準(zhǔn)錯(cuò)誤文件描述符。

請(qǐng)注意,Go 運(yùn)行時(shí)寫入標(biāo)準(zhǔn)錯(cuò)誤以防恐慌和崩潰;關(guān)閉 Stderr 可能會(huì)導(dǎo)致這些消息到其他地方,也許會(huì)導(dǎo)致稍后打開的文件。

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 將當(dāng)前工作目錄更改為指定的目錄。如果有錯(cuò)誤,它將是 * PathError 類型。

func Chmod

func Chmod(name string, mode FileMode) error

Chmod 將指定文件的模式更改為模式。如果文件是符號(hào)鏈接,它將更改鏈接目標(biāo)的模式。如果有錯(cuò)誤,它將是 * PathError 類型。

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

在 Unix 上,使用模式的權(quán)限位 ModeSetuid,ModeSetgid 和 ModeSticky 。

在 Windows 上,模式必須是非零的,否則只能使用0200位(所有者可寫)模式; 它控制文件的只讀屬性是設(shè)置還是清除。屬性。其他位當(dāng)前未使用。對(duì)于只讀文件使用模式0400,對(duì)于可讀+可寫文件使用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

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

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

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

func Chtimes

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

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

底層文件系統(tǒng)可能會(huì)將值截?cái)嗷蛏崛氲讲惶_的時(shí)間單位。如果有錯(cuò)誤,它將是 * 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í)行文件返回啟動(dòng)當(dāng)前進(jìn)程的可執(zhí)行文件的路徑名稱。不能保證路徑仍然指向正確的可執(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

func Exit(code int)

退出時(shí)會(huì)導(dǎo)致當(dāng)前程序退出并顯示給定的狀態(tài)碼。通常,代碼0表示成功,錯(cuò)誤不為零。該程序立即終止; 延遲功能不運(yùn)行。

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ù)當(dāng)前環(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ū)分空值和未設(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

func Geteuid() int

Geteuid 返回調(diào)用者的數(shù)字有效用戶標(biā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 。請(qǐng)參閱 os/user 軟件包以獲取可能的替代方案。

func Getpagesize

func Getpagesize() int

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

func Getpid

func Getpid() int

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

func Getppid

func Getppid() int

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

func Getuid

func Getuid() int

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

在 Windows 上,它返回-1。

func Getwd

func Getwd() (dir string, err error)

Getwd 返回與當(dāng)前目錄對(duì)應(yīng)的根路徑名稱。如果當(dāng)前目錄可以通過(guò)多個(gè)路徑到達(dá)(由于符號(hào)鏈接),Getwd 可以返回其中任何一個(gè)。

func Hostname

func Hostname() (name string, err error)

主機(jī)名返回內(nèi)核報(bào)告的主機(jī)名。

func IsExist

func IsExist(err error) bool

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

func IsNotExist

func IsNotExist(err error) bool

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

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 報(bào)告 c 是否是目錄分隔符。

func IsPermission

func IsPermission(err error) bool

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

func Lchown

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

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

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

func Link

func Link(oldname, newname string) error

鏈接創(chuàng)建新名稱作為舊名稱文件的硬鏈接。如果有錯(cuò)誤,它將是 * 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 使用指定的名稱和權(quán)限位創(chuàng)建一個(gè)新目錄。如果有錯(cuò)誤,它將是 * PathError 類型。

func MkdirAll

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

func NewSyscallError(syscall string, err error) error

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

func Readlink

func Readlink(name string) (string, error)

Readlink 返回指定符號(hào)鏈接的目的地。如果有錯(cuò)誤,它將是 * PathError 類型。

func Remove

func Remove(name string) error

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

func RemoveAll

func RemoveAll(path string) error

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

func Rename

func Rename(oldpath, newpath string) error

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

func SameFile

func SameFile(fi1, fi2 FileInfo) bool

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

func Setenv

func Setenv(key, value string) error

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

func Symlink

func Symlink(oldname, newname string) error

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

func TempDir

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

func Truncate(name string, size int64) error

截?cái)喔闹付ㄎ募拇笮 H绻募欠?hào)鏈接,它將更改鏈接目標(biāo)的大小。如果有錯(cuò)誤,它將是 * PathError 類型。

func Unsetenv

func Unsetenv(key string) error

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

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

type File

文件表示一個(gè)打開的文件描述符。

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

func Create

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

Create 使用模式0666(在 umask 之前)創(chuàng)建命名文件,如果它已經(jīng)存在,則截?cái)嗨H绻晒?,返回文件上的方法可用?I/O ; 關(guān)聯(lián)的文件描述符具有模式 O_RDWR 。如果有錯(cuò)誤,它將是 * PathError 類型。

func NewFile

func NewFile(fd uintptr, name string) *File

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

func Open

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

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

func OpenFile

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

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

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

func (*File) Chdir

func (f *File) Chdir() error

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

func (*File) Chmod

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

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

func (*File) Chown

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

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

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

func (*File) Close

func (f *File) Close() error

關(guān)閉關(guān)閉文件,使其不能用于 I/O 。它返回一個(gè)錯(cuò)誤,如果有的話。

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) 個(gè)字節(jié)。它返回讀取的字節(jié)數(shù)和遇到的任何錯(cuò)誤。在文件結(jié)尾,Read 返回0, io.EOF 。

func (*File) ReadAt

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

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

func (*File) Readdir

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 (*File) Readdirnames

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

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

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

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

func (*File) Seek

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

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

func (*File) Stat

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

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

func (*File) Sync

func (f *File) Sync() error

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

func (*File) Truncate

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

截?cái)喔奈募拇笮?。它不?huì)更改 I/O 偏移量。如果有錯(cuò)誤,它將是 * PathError 類型。

func (*File) Write

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

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

func (*File) WriteAt

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

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

func (*File) WriteString

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

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

type FileInfo

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

func Lstat(name string) (FileInfo, error)

Lstat 返回一個(gè)描述指定文件的 FileInfo 。如果文件是符號(hào)鏈接,則返回的 FileInfo 描述符號(hào)鏈接。Lstat 不會(huì)嘗試跟隨鏈接。如果有錯(cuò)誤,它將是 * PathError 類型。

func Stat

func Stat(name string) (FileInfo, error)

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

type FileMode

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é)議或磁盤表示:盡管可能會(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 (FileMode) IsDir

func (m FileMode) IsDir() bool

IsDir 報(bào)告 m 是否描述目錄。也就是說(shuō),它測(cè)試 ModeDir 位在 m 中設(shè)置。

func (FileMode) IsRegular

func (m FileMode) IsRegular() bool

IsRegular 報(bào)告 m 是否描述常規(guī)文件。也就是說(shuō),它測(cè)試沒(méi)有設(shè)置模式類型位。

func (FileMode) Perm

func (m FileMode) Perm() FileMode

Perm 以 m 為單位返回 Unix 權(quán)限位。

func (FileMode) String

func (m FileMode) String() string

type LinkError

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

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

func (*LinkError) Error

func (e *LinkError) Error() string

type PathError

PathError 記錄錯(cuò)誤以及導(dǎo)致它的操作和文件路徑。

type PathError struct {
        Op   string
        Path string
        Err  error}

func (*PathError) Error

func (e *PathError) Error() string

type ProcAttr

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}

type Process

進(jìn)程存儲(chǔ)有關(guān)由 StartProcess 創(chuàng)建的進(jìn)程的信息。

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

func FindProcess

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

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 類型。

func (*Process) Kill

func (p *Process) Kill() error

殺死導(dǎo)致進(jìn)程立即退出。

func (*Process) Release

func (p *Process) Release() error

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

func (*Process) Signal

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

信號(hào)向過(guò)程發(fā)送信號(hào)。未在Windows上發(fā)送中斷。

func (*Process) Wait

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

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

type ProcessState

ProcessState 存儲(chǔ)有關(guān)由 Wait 報(bào)告的進(jìn)程的信息。

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

func (*ProcessState) Exited

func (p *ProcessState) Exited() bool

已退出的報(bào)告是否已退出該程序。

func (*ProcessState) Pid

func (p *ProcessState) Pid() int

Pid 返回退出進(jìn)程的進(jìn)程 ID 。

func (*ProcessState) String

func (p *ProcessState) String() string

func (*ProcessState) Success

func (p *ProcessState) Success() bool

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

func (*ProcessState) Sys

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

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

func (*ProcessState) SysUsage

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

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

func (*ProcessState) SystemTime

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

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

func (*ProcessState) UserTime

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

UserTime 返回退出進(jìn)程及其子進(jìn)程的用戶 CPU 時(shí)間。

type Signal

信號(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)

type SyscallError

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

type SyscallError struct {
        Syscall string
        Err     error}

func (*SyscallError) Error

func (e *SyscallError) Error() string
上一篇: 下一篇: