Documentation ¶
Overview ¶
Package util provides small utility functions.
Index ¶
- func CheckErr(err error, msg ...interface{})
- func IsTerminal(file *os.File) bool
- func WarnErr(err error, msg ...interface{}) error
- type LinesReader
- func NewLinesReaderFromFile(filename string) (r *LinesReader, err error)
- func NewLinesReaderFromReader(rd io.Reader) (r *LinesReader)
- func NewLinesReaderSizeFromFile(filename string, bufsize int) (r *LinesReader, err error)
- func NewLinesReaderSizeFromReader(rd io.Reader, bufsize int) (r *LinesReader)
- type ReadCloser
- type Reader
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type LinesReader ¶
type LinesReader struct {
// contains filtered or unexported fields
}
### Opening a LinesReader ###
Example 1:
r, err := util.NewLinesReaderFromFile(filename) util.CheckErr(err)
Example 2:
r = util.NewLinesReaderFromReader(os.Stdin)
Example 3:
f, e := os.Open(filename) util.CheckErr(e) defer f.Close() rd, e := gzip.NewReader(f) util.CheckErr(e) defer rd.Close() r = util.NewLinesReaderFromReader(rd)
Since NewLinesReaderFromFile also handles .gz and .bz2 directly, you can use example 1 instead of example 3 for these files.
### Using a LinesReader ###
Example 1:
for line := range r.ReadLines() { // do something with line }
Example 2:
for line := range r.ReadLines() { // do something with line // if you need to stop before all lines are read: r.Break() break // not needed at bottom of loop // do more things }
func NewLinesReaderFromFile ¶
func NewLinesReaderFromFile(filename string) (r *LinesReader, err error)
Either plain text file, gzip'ed text file with name ending in .gz, or bzip2'ed text file with name ending in .bz2
func NewLinesReaderFromReader ¶
func NewLinesReaderFromReader(rd io.Reader) (r *LinesReader)
func NewLinesReaderSizeFromFile ¶
func NewLinesReaderSizeFromFile(filename string, bufsize int) (r *LinesReader, err error)
Either plain text file, gzip'ed text file with name ending in .gz, or bzip2'ed text file with name ending in .bz2
func NewLinesReaderSizeFromReader ¶
func NewLinesReaderSizeFromReader(rd io.Reader, bufsize int) (r *LinesReader)
func (*LinesReader) Break ¶
func (r *LinesReader) Break()
func (*LinesReader) ReadLines ¶
func (r *LinesReader) ReadLines() <-chan string
func (*LinesReader) ReadLinesBytes ¶
func (r *LinesReader) ReadLinesBytes() <-chan []byte
type ReadCloser ¶
type ReadCloser struct {
// contains filtered or unexported fields
}
func Open ¶
func Open(filename string) (rc *ReadCloser, err error)
Opens for reading a plain file, gzip'ed file (extension .gz), or bzip2'ed file (extension .bz2)
func (ReadCloser) Close ¶
func (rc ReadCloser) Close() (err error)
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
Type implementing a robust line reader.
func NewReaderSize ¶
Create new reader with specified buffer size.
func (*Reader) ReadLine ¶
Read single line from Reader, without EOL. EOL can be any of: \n \r \r\n \n\r. Last line without EOL is allowed. Result is only valid until next call to ReadLine() or ReadLineString()
func (*Reader) ReadLineString ¶
Same as ReadLine(), but returns string instead of []byte. Result stays valid.