|
23 | 23 | 下面是演示代码:
|
24 | 24 | ```Go
|
25 | 25 |
|
26 |
| - package main |
27 |
| - |
28 |
| - import ( |
29 |
| - "fmt" |
30 |
| - "os" |
31 |
| - ) |
32 |
| - |
33 |
| - func main() { |
34 |
| - os.Mkdir("astaxie", 0777) |
35 |
| - os.MkdirAll("astaxie/test1/test2", 0777) |
36 |
| - err := os.Remove("astaxie") |
37 |
| - if err != nil { |
38 |
| - fmt.Println(err) |
39 |
| - } |
40 |
| - os.RemoveAll("astaxie") |
| 26 | +package main |
| 27 | + |
| 28 | +import ( |
| 29 | + "fmt" |
| 30 | + "os" |
| 31 | +) |
| 32 | + |
| 33 | +func main() { |
| 34 | + os.Mkdir("astaxie", 0777) |
| 35 | + os.MkdirAll("astaxie/test1/test2", 0777) |
| 36 | + err := os.Remove("astaxie") |
| 37 | + if err != nil { |
| 38 | + fmt.Println(err) |
41 | 39 | }
|
| 40 | + os.RemoveAll("astaxie") |
| 41 | +} |
42 | 42 |
|
43 | 43 | ```
|
44 | 44 |
|
|
84 | 84 | 写文件的示例代码
|
85 | 85 | ```Go
|
86 | 86 |
|
87 |
| - package main |
| 87 | +package main |
88 | 88 |
|
89 |
| - import ( |
90 |
| - "fmt" |
91 |
| - "os" |
92 |
| - ) |
93 |
| - |
94 |
| - func main() { |
95 |
| - userFile := "astaxie.txt" |
96 |
| - fout, err := os.Create(userFile) |
97 |
| - if err != nil { |
98 |
| - fmt.Println(userFile, err) |
99 |
| - return |
100 |
| - } |
101 |
| - defer fout.Close() |
102 |
| - for i := 0; i < 10; i++ { |
103 |
| - fout.WriteString("Just a test!\r\n") |
104 |
| - fout.Write([]byte("Just a test!\r\n")) |
105 |
| - } |
| 89 | +import ( |
| 90 | + "fmt" |
| 91 | + "os" |
| 92 | +) |
| 93 | + |
| 94 | +func main() { |
| 95 | + userFile := "astaxie.txt" |
| 96 | + fout, err := os.Create(userFile) |
| 97 | + if err != nil { |
| 98 | + fmt.Println(userFile, err) |
| 99 | + return |
106 | 100 | }
|
| 101 | + defer fout.Close() |
| 102 | + for i := 0; i < 10; i++ { |
| 103 | + fout.WriteString("Just a test!\r\n") |
| 104 | + fout.Write([]byte("Just a test!\r\n")) |
| 105 | + } |
| 106 | +} |
107 | 107 |
|
108 | 108 | ```
|
109 | 109 | ### 读文件
|
|
120 | 120 | 读文件的示例代码:
|
121 | 121 | ```Go
|
122 | 122 |
|
123 |
| - package main |
| 123 | +package main |
124 | 124 |
|
125 |
| - import ( |
126 |
| - "fmt" |
127 |
| - "os" |
128 |
| - ) |
129 |
| - |
130 |
| - func main() { |
131 |
| - userFile := "asatxie.txt" |
132 |
| - fl, err := os.Open(userFile) |
133 |
| - if err != nil { |
134 |
| - fmt.Println(userFile, err) |
135 |
| - return |
136 |
| - } |
137 |
| - defer fl.Close() |
138 |
| - buf := make([]byte, 1024) |
139 |
| - for { |
140 |
| - n, _ := fl.Read(buf) |
141 |
| - if 0 == n { |
142 |
| - break |
143 |
| - } |
144 |
| - os.Stdout.Write(buf[:n]) |
| 125 | +import ( |
| 126 | + "fmt" |
| 127 | + "os" |
| 128 | +) |
| 129 | + |
| 130 | +func main() { |
| 131 | + userFile := "asatxie.txt" |
| 132 | + fl, err := os.Open(userFile) |
| 133 | + if err != nil { |
| 134 | + fmt.Println(userFile, err) |
| 135 | + return |
| 136 | + } |
| 137 | + defer fl.Close() |
| 138 | + buf := make([]byte, 1024) |
| 139 | + for { |
| 140 | + n, _ := fl.Read(buf) |
| 141 | + if 0 == n { |
| 142 | + break |
145 | 143 | }
|
| 144 | + os.Stdout.Write(buf[:n]) |
146 | 145 | }
|
147 |
| - |
| 146 | +} |
| 147 | + |
148 | 148 | ```
|
149 | 149 | ### 删除文件
|
150 | 150 | Go语言里面删除文件和删除文件夹是同一个函数
|
|
0 commit comments