[筆記] Golang File

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
 1 package main 2  3 import ( 4     "fmt" 5     "os" 6 ) 7  8 func main() { 9     demo1()10     demo2()11     demo3()12     demo4()13 }14 15 func demo1() {16     os.Mkdir("astaxie", 0777)17     os.MkdirAll("astaxie/test1/test2", 0777)18     err := os.Remove("astaxie")19     if err != nil {20         fmt.Println(err)21     }22     os.RemoveAll("astaxie")23 }24 25 func demo2() {26     userFile := "astaxie.txt"27     fout, err := os.Create(userFile)28     defer fout.Close()29     if err != nil {30         fmt.Println(userFile, err)31         return32     }33     for i := 0; i < 10; i++ {34         fout.WriteString("just a test!\r\n")35         fout.Write([]byte("just a test!\r\n"))36     }37 }38 39 func demo3() {40     userFile := "astaxie.txt"41     fl, err := os.Open(userFile)42     defer fl.Close()43     if err != nil {44         fmt.Println(userFile, err)45         return46     }47     buf := make([]byte, 1024)48     for {49         n, _ := fl.Read(buf)50         if 0 == n {51             break52         }53         os.Stdout.Write(buf[:n])54     }55 }56 57 func demo4() {58     os.Remove("astaxie.txt")59 }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.