go lang 讀寫檔案操作

2021-06-09 20:34:48 字數 1224 閱讀 2917

參考備份

寫程式離不了檔案操作,

這裡總結下

go語言檔案操作。

一、建立與開啟

建立檔案函式:

func create(name string) (file *file, err error)

func newfile(fd int, name string) *file

具體見官網:

開啟檔案函式:

func open(name string) (file *file, err error)

func openfile(name string, flag int, perm uint32) (file *file, err error)

具體見官網:

二、寫檔案

寫檔案函式:

func (file *file) write(b byte) (n int, err error)

func (file *file) writeat(b byte, off int64) (n int, err error)

func (file *file) writestring(s string) (ret int, err error)

具體見官網:

寫檔案示例**:

package main

import (

"os"

"fmt"

)func main()

for i:= 0;i<10;i++

}

三、讀檔案

讀檔案函式:

func (file *file) read(b byte) (n int, err error)

func (file *file) readat(b byte, off int64) (n int, err error)

具體見官網:

讀檔案示例**:

package main

import (

"os"

"fmt"

)func main()

buf := make(byte, 1024)

foros.stdout.write(buf[:n])

}}

四、刪除檔案

函式:func remove(name string) error

具體見官網:

Golang 檔案讀寫操作

package main import fmt io log os path filepath strconv func main strfile strdir testfile.txt fmt.println file to open is strfile 開啟檔案,如果沒有,那麼建立,設定為讀寫...

golang 檔案讀寫操作

實現php file get contents,file put contents 函式 package php import errors os strings sync var mu sync.mutex unsupportprotocols 暫時不支援的協議型別 var unsupportpr...

Golang對檔案讀寫操作

package main import bufio fmt io os 寫func writefile path string 使用完畢,需要關閉檔案 defer f.close var buf string for i 0 i 5 i fmt.println n n 讀檔案內容方法 func re...