fixedwidth

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2017 License: MIT Imports: 8 Imported by: 10

README

fixedwidth GoDoc Report card

Package fixedwidth provides encoding and decoding for fixed-width formatted Data.

go get github.com/ianlopshire/go-fixedwidth

Decode

// Define some fixed-with data to parse
data := []byte("" +
    "1         Ian                 Lopshire" + "\n" +
    "2         John                Doe" + "\n" +
    "3         Jane                Doe" + "\n")

// Define the format as a struct.
// The fixed start and end position are defined via struct tags: `fixed:"{startPos},{endPos}"`.
// Positions start at 1, the the interval is inclusive.
var people []struct {
    ID        int    `fixed:"1,10"`
    FirstName string `fixed:"11,30"`
    LastName  string `fixed:"31,50"`
}

err := fixedwidth.Unmarshal(data, &people)
if err != nil {
    log.Fatal(err)
}

fmt.Printf("%+v\n", people[0])
fmt.Printf("%+v\n", people[1])
fmt.Printf("%+v\n", people[2])
// Output:
// {ID:1 FirstName:Ian LastName:Lopshire}
// {ID:2 FirstName:John LastName:Doe}
// {ID:3 FirstName:Jane LastName:Doe}

Licence

MIT

Documentation

Overview

Package fixedwidth provides encoding and decoding for fixed-width formatted Data.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Unmarshal

func Unmarshal(data []byte, v interface{}) error

Unmarshal parses the fixed width encoded data and stores the result in the value pointed to by v. If v is nil or not a pointer, Unmarshal returns an InvalidUnmarshalError.

Example
// Define some fixed-with data to parse
data := []byte("" +
	"1         Ian                 Lopshire" + "\n" +
	"2         John                Doe" + "\n" +
	"3         Jane                Doe" + "\n")

// Define the format as a struct.
// The fixed start and end position are defined via struct tags: `fixed:"{startPos},{endPos}"`.
// Positions start at 1, the the interval is inclusive.
var people []struct {
	ID        int    `fixed:"1,10"`
	FirstName string `fixed:"11,30"`
	LastName  string `fixed:"31,50"`
}

err := fixedwidth.Unmarshal(data, &people)
if err != nil {
	log.Fatal(err)
}

fmt.Printf("%+v\n", people[0])
fmt.Printf("%+v\n", people[1])
fmt.Printf("%+v\n", people[2])
Output:

{ID:1 FirstName:Ian LastName:Lopshire}
{ID:2 FirstName:John LastName:Doe}
{ID:3 FirstName:Jane LastName:Doe}

Types

type Decoder

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

A Decoder reads and decodes fixed width data from an input stream.

func NewDecoder

func NewDecoder(r io.Reader) *Decoder

NewDecoder returns a new decoder that reads from r.

func (*Decoder) Decode

func (d *Decoder) Decode(v interface{}) (err error)

Decode reads from its input and stores the decoded data the value pointed to by v.

In the case that v points to a struct value, Decode will read a single line from the input.

In the case that v points to a slice value, Decode will read until the end of its input.

type InvalidUnmarshalError

type InvalidUnmarshalError struct {
	Type reflect.Type
}

An InvalidUnmarshalError describes an invalid argument passed to Unmarshal. (The argument to Unmarshal must be a non-nil pointer.)

func (*InvalidUnmarshalError) Error

func (e *InvalidUnmarshalError) Error() string

type UnmarshalTypeError

type UnmarshalTypeError struct {
	Value  string       // the raw value
	Type   reflect.Type // type of Go value it could not be assigned to
	Struct string       // name of the struct type containing the field
	Field  string       // name of the field holding the Go value
}

An UnmarshalTypeError describes a value that was not appropriate for a value of a specific Go type.

func (*UnmarshalTypeError) Error

func (e *UnmarshalTypeError) Error() string

Jump to

Keyboard shortcuts

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