Help us shape the future of Go! Take the 2026 Go Developer Survey

gofixedfield

package module
v0.0.0-...-c90d58b Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2025 License: BSD-2-Clause Imports: 5 Imported by: 1

README

GOFIXEDFIELD

Build Status Go Report Card GoDoc

Go library to deal with extracting fixed field form values and CSV/delimiter separated values using struct tags.

European-styled numbers

To parse documents that use a comma "," instead of the decimal point, just set to true the corresponding global variable: gofixedfield.DecimalComma = true

Documentation

Index

Constants

View Source
const (
	// EOLUnix represents Unix/Linux style end of line.
	EOLUnix = "\n"
	// EOLMac represents Macintosh style end of line.
	EOLMac = "\r"
	// EOLDOS represents DOS/Windows style end of line.
	EOLDOS = "\r\n"
)

Variables

View Source
var DecimalComma bool

DecimalComma enables the parsing of numeric values having a comma instead of a point as decimal separator.

Functions

func RecordsFromFile

func RecordsFromFile(filename string, eolstyle string) ([]string, error)

RecordsFromFile reads a file and splits into single line records, which can be unmarshalled.

func Unmarshal

func Unmarshal(data string, v any) error

Unmarshal unmarshals string data into an annotated interface. This should resemble:

type SomeType struct {
	ValA string        `fixed:"1-5"`
	ValB int           `fixed:"10-15"`
	ValC *EmbeddedType `fixed:"16-22"`
}
type EmbeddedType struct {
	ValX string `fixed:"1-3"`
	ValY string `fixed:"4-6"`
	ValZ string `fixed:"8"`
}

var out SomeType
err := Unmarshal("some string here", &out)

String offsets are one based, not zero based.

func UnmarshalCsv

func UnmarshalCsv(data string, sep string, v any) error

UnmarshalCsv unmarshals string data into an annotated interface. This should resemble:

type SomeType struct {
	ValA      string        `csv:"1"`
	ValB      int           `csv:"2"`
	ValC      *EmbeddedType `csv:"3" csvsplit:"~"`
	WholeLine string        `csv:"raw"`
}
type EmbeddedType struct {
	ValX string `csv:"1"`
	ValY string `csv:"2"`
}

var out SomeType
err := Unmarshal("A,2,X~Y", ",", &out)

String offsets are one based, not zero based.

Types

This section is empty.

Jump to

Keyboard shortcuts

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