goltsv

package module
v0.0.0-...-84f1da8 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2013 License: BSD-3-Clause Imports: 4 Imported by: 5

README

========
 goltsv
========

LTSV (Labeled Tab Separeted Value) reader/writer for Go.

.. image:: https://drone.io/github.com/ymotongpoo/goltsv/status.png

Example
=======

Reader
------

::

   package main
   
   import (
   	"bytes"
   	"fmt"
   	"github.com/ymotongpoo/goltsv"
   )
      
   func main() {
   	data := `
   egg:たまご	ham:ハム	bread:パン
   gyoza:ぎょうざ	ramen:ラーメン	sushi:すし	yakiniku:焼肉
   yamanashi:山梨	tokyo:東京	okinawa:沖縄	hokkaido:北海道
   `
   	b := bytes.NewBufferString(data)
      
   	// Read LTSV file into map[string]string
   	reader := goltsv.NewReader(b)
   	records, err := reader.ReadAll()
   	if err != nil {
   		panic(err)
   	}
      
   	// dump
   	for i, record := range records {
   		fmt.Printf("===== Data %d\n", i)
   		for k, v := range record {
   			fmt.Printf("\t%s --> %s\n", k, v)
   		}
   	}
   }
   

Writer
------

::

   package main
   
   import (
   	"bytes"
   	"github.com/ymotongpoo/goltsv"
   )
   
   func main() {
   	data := []map[string]string {
   		{"Python": "3.3.0", "Ruby": "2.0 rc2", "Perl": "5.16.2"},
   		{"spam": "foo", "egg": "bar", "ham": "buz"},
   		{"sauce": "ソース", "salt": "しお", "sugar": "さとう", "vinegar": "す"},
   	}
   
   	b := &bytes.Buffer{}
   	writer := goltsv.NewWriter(b)
   	err := writer.WriteAll(data)
   	if err != nil {
   		panic(err)
   	}
   	fmt.Printf("%v", b.String())
   }


License
=======

This packages is distributed under conditions of New BSD License

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrFieldFormat = errors.New("wrong LTSV field format")
	ErrLabelName   = errors.New("unexpected label name")
)

These are the errors that can be returned in ParseError.Error

Functions

This section is empty.

Types

type LTSVReader

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

A Reader reads from a Labeled TSV (LTSV) file.

As returned by NewReader, a Reader expects input conforming LTSV (http://ltsv.org/)

func NewReader

func NewReader(r io.Reader) *LTSVReader

NewReader returns a new LTSVReader that reads from r.

func (*LTSVReader) Read

func (r *LTSVReader) Read() (record map[string]string, err error)

Read reads one record from r. The record is a map of string with each key and value representing one field.

func (*LTSVReader) ReadAll

func (r *LTSVReader) ReadAll() (records []map[string]string, err error)

ReadAll reads all the remainig records from r. Each records is a slice of map of fields. TODO(ymotongpoo): compare with the case of using csv.ReadAll()

type LTSVWriter

type LTSVWriter struct {
	UseCRLF bool
	// contains filtered or unexported fields
}

A Writer wites records to a LTSV encoded file.

As returned by NewWriter, a Writer writes records terminated by a newline and uses '\t' as the field delimiter. Detailed format is described in LTSV official web site. (http://ltsv.org/)

func NewWriter

func NewWriter(w io.Writer) *LTSVWriter

NewWriter returns a new Writer that writes to w.

func (*LTSVWriter) Flush

func (w *LTSVWriter) Flush() (err error)

Flush writes bufferd data to the underlying bufio.Writer

func (*LTSVWriter) Write

func (w *LTSVWriter) Write(record map[string]string) (err error)

LTSVWriter writes a single LTSV record to w. A record is a map of label and value. TODO(ymotongpoo): add any feature to organize order of field.

func (*LTSVWriter) WriteAll

func (w *LTSVWriter) WriteAll(records []map[string]string) (err error)

WriteAll writes multiple LTSV records to w using Write and then calls Flush.

Jump to

Keyboard shortcuts

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