Documentation ¶
Index ¶
- type Reader
- func (tr *Reader) Bytes() []byte
- func (tr *Reader) Date() time.Time
- func (tr *Reader) DateTime() time.Time
- func (tr *Reader) Error() error
- func (tr *Reader) Float32() float32
- func (tr *Reader) Float64() float64
- func (tr *Reader) HasCols() bool
- func (tr *Reader) Int() int
- func (tr *Reader) Int16() int16
- func (tr *Reader) Int32() int32
- func (tr *Reader) Int64() int64
- func (tr *Reader) Int8() int8
- func (tr *Reader) Next() bool
- func (tr *Reader) Reset(r io.Reader)
- func (tr *Reader) ResetError()
- func (tr *Reader) SkipCol()
- func (tr *Reader) String() string
- func (tr *Reader) Uint() uint
- func (tr *Reader) Uint16() uint16
- func (tr *Reader) Uint32() uint32
- func (tr *Reader) Uint64() uint64
- func (tr *Reader) Uint8() uint8
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
Reader reads tab-separated data.
Call New for creating new TSV reader. Call Next before reading the next row.
It is expected that columns are separated by tabs while rows are separated by newlines.
Example ¶
package main import ( "bytes" "fmt" "github.com/valyala/tsvreader" ) func main() { bs := bytes.NewBufferString( "foo\t42\n" + "bar\t123\n") r := tsvreader.New(bs) for r.Next() { col1 := r.String() col2 := r.Int() fmt.Printf("col1=%s, col2=%d\n", col1, col2) } if err := r.Error(); err != nil { fmt.Printf("unexpected error: %s", err) } }
Output: col1=foo, col2=42 col1=bar, col2=123
func (*Reader) Bytes ¶
Bytes returns the next bytes column value from the current row.
The returned value is valid until the next call to Reader.
func (*Reader) Date ¶
Date returns the next date column value from the current row.
date must be in the format YYYY-MM-DD
func (*Reader) DateTime ¶
DateTime returns the next datetime column value from the current row.
datetime must be in the format YYYY-MM-DD hh:mm:ss.
func (*Reader) HasCols ¶
HasCols returns true if the current row contains unread columns.
An empty row doesn't contain columns.
This function may be used if TSV stream contains rows with different number of colums.
Example ¶
package main import ( "bytes" "fmt" "github.com/valyala/tsvreader" ) func main() { bs := bytes.NewBufferString( "foo\n" + "bar\tbaz\n" + "\n" + "a\tb\tc\n") r := tsvreader.New(bs) for r.Next() { for r.HasCols() { s := r.String() fmt.Printf("%q,", s) } fmt.Printf("\n") } if err := r.Error(); err != nil { fmt.Printf("unexpected error: %s", err) } }
Output: "foo", "bar","baz", "a","b","c",
func (*Reader) Next ¶
Next advances to the next row.
Returns true if the next row does exist.
Next must be called after reading all the columns on the previous row. Check Error after Next returns false.
HasCols may be used for reading rows with variable number of columns.
Example ¶
package main import ( "bytes" "fmt" "github.com/valyala/tsvreader" ) func main() { bs := bytes.NewBufferString("1\n2\n3\n42\n") r := tsvreader.New(bs) for r.Next() { n := r.Int() fmt.Printf("%d\n", n) } if err := r.Error(); err != nil { fmt.Printf("unexpected error: %s", err) } }
Output: 1 2 3 42
func (*Reader) ResetError ¶
func (tr *Reader) ResetError()
ResetError resets the current error, so the reader could proceed further.
func (*Reader) SkipCol ¶
func (tr *Reader) SkipCol()
SkipCol skips the next column from the current row.
func (*Reader) String ¶
String returns the next string column value from the current row.
String allocates memory. Use Bytes to avoid memory allocations.