Reference
Version 2.4.1. Anything not listed here is not part of the public API.
Layout and Field
Name | Field name. Must be unique within a layout; used in lookups and errors only. |
Width | Field width in bytes. Must be greater than zero. |
Type | One of the field types below. |
Scale | Implied decimal places. Decimal only. |
Format | Reference layout string. Timestamp and Date only. |
Pad | SpaceRight (default), SpaceLeft, ZeroLeft. |
Optional | An all-pad field decodes to the zero value instead of an error. |
Layout.RecordLength() returns the sum of the widths.
Layout.Validate() reports a duplicate name, a zero width, a scale on a
non-decimal field, or a missing format on a temporal field. Both readers and writers call
Validate on construction, so an invalid layout fails before any I/O happens.
Field types
Text | Bytes decoded with the reader's encoding, padding trimmed according to Pad. |
Integer | Optionally signed decimal digits. An overpunched sign in the final byte is accepted. |
Decimal | Digits with an implied decimal point at Scale. Exact; never a float. |
Packed | Packed decimal, two digits per byte, sign in the low nibble. |
Date | Calendar date, no time zone. |
Timestamp | Date and time. UTC unless the format carries an offset. |
Raw | Bytes, untouched. Padding is not trimmed and no encoding is applied. |
Reader
NewReader(io.Reader, Layout) *Reader | Constructs a reader. Panics only if the layout is nil. |
Next() bool | Decodes the next record. False at EOF and on the first error. |
Record() Record | The record decoded by the last Next. Invalid after the following call. |
Err() error | The first error encountered, or nil at a clean EOF. |
Offset() int64 | Byte offset of the start of the current record. |
Encoding | ASCII (default), Latin1, UTF8. |
Terminator | None (default), LF, CRLF, Auto. |
BufferSize | Read buffer in bytes, default 262144, rounded up to whole records. |
OnError | func(*PositionError) Action. Return Skip or Stop. |
Record
Text(name) (string, error) | Decoded text. Error if the field is not Text. |
Int(name) (int64, error) | Integer value. Error on any non-digit byte in a required field. |
Decimal(name) (Decimal, error) | Exact decimal with the declared scale. |
Time(name) (time.Time, error) | Parsed with the field's format. |
Bytes(name) ([]byte, error) | The raw field bytes. Aliases the reader's buffer. |
IsBlank(name) bool | True if the field is entirely padding. |
Clone() Record | An independent copy, safe to retain. |
Writer
NewWriter(io.Writer, Layout) *Writer | Constructs a buffered writer. |
Write(Values) error | Encodes one record. A missing required field is an error. |
Flush() error | Writes buffered records to the underlying writer. |
Close() error | Flushes and marks the writer used. Must be checked. |
Terminator | Matches the reader's; default None. |
Errors
ErrShortRecord | The stream ended part-way through a record. |
ErrFieldSyntax | The bytes of a field do not match its declared type. |
ErrFieldOverflow | A value written does not fit its declared width. |
ErrLayout | The layout is self-inconsistent; returned before any I/O. |
ErrEncoding | A byte sequence is not valid in the declared encoding. |
PositionError | Wraps any of the above with Offset, Record, Field. |
All sentinel errors are comparable with errors.Is through the wrapping
PositionError. New sentinels may be added in a minor release; existing ones will
not be removed before 3.0.