码迷,mamicode.com
首页 > 其他好文 > 详细

[笔记] Golang File

时间:2016-05-05 00:18:15      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:

 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         return
32     }
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         return
46     }
47     buf := make([]byte, 1024)
48     for {
49         n, _ := fl.Read(buf)
50         if 0 == n {
51             break
52         }
53         os.Stdout.Write(buf[:n])
54     }
55 }
56 
57 func demo4() {
58     os.Remove("astaxie.txt")
59 }

 

[笔记] Golang File

标签:

原文地址:http://www.cnblogs.com/loveyx/p/5460218.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!