func Create(name string) (file *File, err error) 直接通過紋面創建文件 func NewFile(fd uintptr, name string) *File func Open(name string) (file *File, err error) 以只讀方式打開一個存在的文件,打開就可以讀取了。 func OpenFile(name string, flag int, perm FileMode) (file *File, err 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) 從off開始讀取文件 func (f *File) Readdir(n int) (fi []FileInfo, err error) 讀取文件目錄返回n個fileinfo func (f *File) Readdirnames(n int) (names []string, err error) 讀取文件目錄返回n個文件名 func (f *File) Seek(offset int64, whence int) (ret int64, err error) 設置讀寫文件的偏移量,whence為0表示相對於文件的開始處,1表示相對於當前的位置,2表示相對於文件結尾。他返回偏移量。如果有錯誤返回錯誤 func (f *File) Stat() (fi FileInfo, err error) 返回當前文件fileinfo結構體 func (f *File) Sync() (err 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) 在offset位置寫入內容 func (f *File) WriteString(s string) (ret int, err error) 寫入字符
案例地址:
http://godeye.org/index.php?a=lesson&id=85
注意的一點是: 文件如果在文件夾📂中,要用絕對路徑