Documentation ¶
Overview ¶
Package gtsv is the package to read TSV formatted text.
It is designed to type-specifically use each tsv columns, and easily find row and column number which error happened.
This is the example to treat valid TSV.
package main import ( "bytes" "fmt" "github.com/yagi5/gtsv" ) func main() { tsv := "1\t2.1\ta\n" + "4\t5.2\tb\n" + "7\t8.3\tc\n" gt := gtsv.New(bytes.NewBufferString(tsv)) for gt.Next() { fmt.Println(gt.Int()) fmt.Println(gt.Float64()) fmt.Println(gt.String()) } fmt.Println(gt.Error()) }
While gt.Next() returns true, you can load column value type-specific, like `gt.Int()` , `gt.String()` .
Of course, you can use this with struct.
type user struct { name string age int male bool } func main() { tsv := "john\t18\ttrue\n" + "emily\t16\tfalse\n" gt := gtsv.New(bytes.NewBufferString(tsv)) var users []*user for gt.Next() { users = append(users, &user{name: gt.String(), age: gt.Int(), male: gt.Bool()}) } fmt.Println(gr.Error()) }
If you change the order to call `gt.String()` and `gt.Int()` , `gt.Error()` will be non-nil because it's not the same as TSV column order.
Sometimes, we want to know the error position of these files. If you need it, you can cast `gt.Error()` (this is just a `error`) into `gtsv.Error` to know error position. like this:
tsvInvalid := "1\t2.1\ta\n" + "4\t5.2\tb\n" + "a\t8.3\tc\n" // first column is not int gt := gtsv.New(bytes.NewBufferString(tsvInvalid)) for gt.Next() { fmt.Println(gt.Int()) fmt.Println(gt.Float64()) fmt.Println(gt.String()) } if err := gt.Error(); err != nil { er := err.(gtsv.Error) fmt.Printf("error row: %d, col: %d", er.Row(), er.Col()) }
if cast `err.(gtsv.Error)` succeeded, `Row()` and `Col()` show you error position.
Index ¶
- type Error
- type Reader
- func (gr *Reader) Bool() bool
- func (gr *Reader) Bytes() []byte
- func (gr *Reader) Error() error
- func (gr *Reader) Float32() float32
- func (gr *Reader) Float64() float64
- func (gr *Reader) Int() int
- func (gr *Reader) Int16() int16
- func (gr *Reader) Int32() int32
- func (gr *Reader) Int64() int64
- func (gr *Reader) Int8() int8
- func (gr *Reader) Next() bool
- func (gr *Reader) String() string
- func (gr *Reader) Uint() uint
- func (gr *Reader) Uint16() uint16
- func (gr *Reader) Uint32() uint32
- func (gr *Reader) Uint64() uint64
- func (gr *Reader) Uint8() uint8
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Error ¶
Error is the error interface. If `gt.Error()` returned non-nil, usually it implements this interface. So, Row() and Col() will return error position.
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
Reader contains some fields to store tsv-reading-information. It shouldn't be used by client so unexported.
func (*Reader) Bool ¶
Bool returns next column as bool. If error had happened, it always returns false. Bool() uses `strconv.ParseBool()` inside, so can read 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False. If any other value, it will be false.
func (*Reader) Bytes ¶
Bytes returns next column as []byte. If error had happened, it always returns nil. Escape sequences will be unescaped.
func (*Reader) Error ¶
Error returns TSV reading error. If something is wrong, Error() returns error. It is important to know one thing, that `Error()` doesn't returns io.EOF. If `Next()` returned false && and `Error()` returned nil, reading TSV is correctly finished.
func (*Reader) Float32 ¶
Float32 returns next column as float32. If error had happened, it always returns zero-value.
func (*Reader) Float64 ¶
Float64 returns next column as float64. If error had happened, it always returns zero-value.
func (*Reader) Int ¶
Int returns next column as int. If error had happened, it always returns zero-value.
func (*Reader) Int16 ¶
Int16 returns next column as int16. If error had happened, it always returns zero-value.
func (*Reader) Int32 ¶
Int32 returns next column as int32. If error had happened, it always returns zero-value.
func (*Reader) Int64 ¶
Int64 returns next column as int64. If error had happened, it always returns zero-value.
func (*Reader) Int8 ¶
Int8 returns next column as int8. If error had happened, it always returns zero-value.
func (*Reader) Next ¶
Next returns true when next row exists. It's expected to use with `for` . If error had happened, `Next()` returns always false.
func (*Reader) Uint ¶
Uint returns next column as uint. If error had happened, it always returns zero-value.
func (*Reader) Uint16 ¶
Uint16 returns next column as uint16. If error had happened, it always returns zero-value.
func (*Reader) Uint32 ¶
Uint32 returns next column as uint32. If error had happened, it always returns zero-value.