util

package module
v0.9.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 20, 2021 License: BSD-2-Clause Imports: 11 Imported by: 23

README

Go package util provides small utility functions.

Install

go get github.com/pebbe/util

Docs

Documentation

Overview

Package util provides small utility functions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckErr

func CheckErr(err error, msg ...interface{})

func IsTerminal

func IsTerminal(file *os.File) bool

Examples:

IsTerminal(os.Stdin)
IsTerminal(os.Stdout)
IsTerminal(os.Stderr)

func WarnErr

func WarnErr(err error, msg ...interface{}) error

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)

func (ReadCloser) Read

func (rc ReadCloser) Read(p []byte) (n int, err error)

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

Type implementing a robust line reader.

func NewReader

func NewReader(rd io.Reader) *Reader

Create new reader with default buffer size.

func NewReaderSize

func NewReaderSize(rd io.Reader, size int) *Reader

Create new reader with specified buffer size.

func (*Reader) ReadLine

func (r *Reader) ReadLine() (line []byte, err error)

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

func (r *Reader) ReadLineString() (line string, err error)

Same as ReadLine(), but returns string instead of []byte. Result stays valid.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL