-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.go
50 lines (44 loc) · 1.45 KB
/
file.go
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright 2015 Michael O'Rourke. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package drain
import (
"bufio"
"io"
"os"
)
// Scans all lines from a File into a channel until EOF
//
func FileLinesToChan(fileName string, dest chan<- string) error {
return FileToChan(fileName, bufio.ScanLines, dest)
}
// Scans all tokens from a File into a channel until EOF
//
func FileToChan(fileName string, split bufio.SplitFunc, dest chan<- string) (err error) {
var f io.ReadCloser
if f, err = os.Open(fileName); err != nil {
return
}
defer f.Close()
err = ReaderToChan(f, split, dest)
return
}
// Writes all strings from a channel to a File, with trailing newline characters, until the channel is closed.
// Uses the default buffer size.
//
func ChanToFile(source <-chan string, fileName string) error {
return ChanToFileSize(source, DefaultBufferSize, fileName)
}
// Writes all strings from a channel to a File, with trailing newline characters, until the channel is closed.
// The file will be created if it does not exist, and appended to if it already exists.
//
func ChanToFileSize(source <-chan string, bufSize int, fileName string) (err error) {
var f io.WriteCloser
if f, err = os.OpenFile(fileName, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0664); err != nil {
return
}
defer f.Close()
bw := bufio.NewWriterSize(f, bufSize)
err = ChanToBufioWriter(source, NewLineString, bw)
return
}