golang檔案讀寫

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

讀寫檔案要用到os包中的

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

該方法第一個參數為檔案路徑,第二個參數控制檔案的開啟檔案,第三個參數控制檔案模式

可用的開啟檔案有

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.)

開啟模式

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 (not backed up) 臨時檔案    ModeSymlink                        // L: symbolic link 象徵性的關聯    ModeDevice                        // D: device file 裝置檔案    ModeNamedPipe                        // p: named pipe (FIFO) 具名管道    ModeSocket                        // S: Unix domain socket  Unix 主機 socket    ModeSetuid                        // u: setuid 設定uid    ModeSetgid                        // g: setgid 設定gid    ModeCharDevice                        // c: Unix character device, when ModeDevice is set Unix 字元裝置,當裝置模式是設定Unix    ModeSticky                        // t: sticky 粘性的    // Mask for the type bits. For regular files, none will be set. bit位遮蓋.不變的檔案設定為none    ModeType    = ModeDir | ModeSymlink | ModeNamedPipe | ModeSocket | ModeDevice    ModePerm    FileMode    = 0777    // Unix permission bits 許可權位.)

建立一個檔案並追加內容

package mainimport (    "os")func main() {    fname := "/home/stack/tmp/t.txt"    f, err := os.OpenFile(fname, os.O_CREATE|os.O_RDWR|os.O_APPEND, os.ModeAppend|os.ModePerm)    if err != nil {        fmt.Println(err)    }    f.WriteString("test")    f.Close()}

刪除檔案

    os.Remove(fname)

建立目錄

    dname := "/home/stack/tmp/d"    os.Mkdir(dname, os.ModeDir|os.ModePerm)

建立完整目錄路徑,即中間目錄不存在的話也一起建立

    os.MkdirAll(dname,os.ModeDir|os.ModePerm)

刪除目錄與刪除檔案相同.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.