@@ -187,7 +187,6 @@ func main() {
187187 f, err := os.Open("users.csv")
188188
189189 if err != nil {
190-
191190 log.Fatal(err)
192191 }
193192
@@ -239,13 +238,13 @@ func main() {
239238 }
240239
241240 f, err := os.Create("users.csv")
242- defer f.Close()
243241
244242 if err != nil {
245-
246- log.Fatalln("failed to open file", err)
243+ log.Fatalln("failed to create file", err)
247244 }
248245
246+ defer f.Close()
247+
249248 w := csv.NewWriter(f)
250249 defer w.Flush()
251250
@@ -265,40 +264,39 @@ func main() {
265264تابع WriteAll چندین رکورد CSV را با استفاده از Write برای writer مینویسد و سپس Flush را فراخوانی میکند.
266265
267266{{< play >}}
268-
269267//write_all.go//
270268
271269package main
272270
273271import (
274- "encoding/csv"
275- "log"
276- "os"
272+ "encoding/csv"
273+ "log"
274+ "os"
277275)
278276
279277func main() {
280278
281- records := [][]string{
282- {"first_name", "last_name", "occupation"},
283- {"John", "Doe", "gardener"},
284- {"Lucy", "Smith", "teacher"},
285- {"Brian", "Bethamy", "programmer"},
286- }
279+ records := [][]string{
280+ {"first_name", "last_name", "occupation"},
281+ {"John", "Doe", "gardener"},
282+ {"Lucy", "Smith", "teacher"},
283+ {"Brian", "Bethamy", "programmer"},
284+ }
287285
288- f, err := os.Create("users.csv")
289- defer f.Close()
286+ f, err := os.Create("users.csv")
290287
291- if err != nil {
288+ if err != nil {
289+ log.Fatalln("failed to create file", err)
290+ }
292291
293- log.Fatalln("failed to open file", err)
294- }
292+ defer f.Close()
295293
296- w := csv.NewWriter(f)
297- err = w.WriteAll(records) // calls Flush internally
294+ w := csv.NewWriter(f)
295+ err = w.WriteAll(records) // calls Flush internally
298296
299- if err != nil {
300- log.Fatal(err)
301- }
297+ if err != nil {
298+ log.Fatal(err)
299+ }
302300}
303301{{< /play >}}
304302
0 commit comments