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 Marshal ¶ added in v0.2.0
Marshal returns the fixed-width encoding of v.
v must be an encodable type or a slice of an encodable type. If v is a slice, each item will be treated as a line. If v is a single encodable type, a single line will be encoded.
In order for a type to be encodable, it must implement the encoding.TextMarshaler interface or be based on one of the following builtin types: string, int, int64, int32, int16, int8, float64, float32, or struct. Pointers to encodable types and interfaces containing encodable types are also encodable.
nil pointers and interfaces will be omitted. zero vales will be encoded normally.
A struct is encoded to a single slice of bytes. Each field in a struct will be encoded and placed at the position defined by its struct tags. The tags should be formatted as `fixed:"{startPos},{endPos}"`. Positions start at 1. The interval is inclusive. Fields without tags and Fields of an un-encodable type are ignored.
If the encoded value of a field is longer than the length of the position interval, the overflow is truncated.
Example ¶
// define some data to encode people := []struct { ID int `fixed:"1,5"` FirstName string `fixed:"6,15"` LastName string `fixed:"16,25"` Grade float64 `fixed:"26,30"` }{ {1, "Ian", "Lopshire", 99.5}, } data, err := Marshal(people) if err != nil { log.Fatal(err) } fmt.Printf("%s", data)
Output: 1 Ian Lopshire 99.50
func Unmarshal ¶
Unmarshal parses 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 the format var people []struct { ID int `fixed:"1,5"` FirstName string `fixed:"6,15"` LastName string `fixed:"16,25"` Grade float64 `fixed:"26,30"` } // define some fixed-with data to parse data := []byte("" + "1 Ian Lopshire 99.50" + "\n" + "2 John Doe 89.50" + "\n" + "3 Jane Doe 79.50" + "\n") err := 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 Grade:99.5} {ID:2 FirstName:John LastName:Doe Grade:89.5} {ID:3 FirstName:Jane LastName:Doe Grade:79.5}
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 ¶
NewDecoder returns a new decoder that reads from r.
type Encoder ¶ added in v0.2.0
type Encoder struct {
// contains filtered or unexported fields
}
An Encoder writes fixed-width formatted data to an output stream.
func NewEncoder ¶ added in v0.2.0
NewEncoder returns a new encoder that writes to w.
type InvalidUnmarshalError ¶
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 MarshalInvalidTypeError ¶ added in v0.2.0
type MarshalInvalidTypeError struct {
// contains filtered or unexported fields
}
MarshalInvalidTypeError describes an invalid type being marshaled.
func (*MarshalInvalidTypeError) Error ¶ added in v0.2.0
func (e *MarshalInvalidTypeError) 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 Cause error // original error }
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