-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwriter.go
More file actions
35 lines (30 loc) · 827 Bytes
/
writer.go
File metadata and controls
35 lines (30 loc) · 827 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package csv
import (
"encoding/csv"
"os"
)
// Writer writes records to a CSV file. It is composed of a csv.Writer from
// the standard library. See it for more information on writing the CSV file.
type Writer struct {
*csv.Writer
closer func() error
}
// Create creates a new CSV file, truncating the file if it already exists.
// If there is an error, it will be of type *PathError.
func Create(name string) (*Writer, error) {
f, err := os.Create(name)
if err != nil {
return nil, err
}
return &Writer{
Writer: csv.NewWriter(f),
closer: f.Close,
}, nil
}
// Close will close the file created by the CSV writer. Flush will be called to
// flush any outstanding buffered data. The CSV contents will not be writable after
// closing the file.
func (w *Writer) Close() error {
w.Flush()
return w.closer()
}