go-fixedwidth alternatives and similar packages
Based on the "Specific Formats" category.
Alternatively, view go-fixedwidth alternatives based on common mentions on social networks and blogs.
-
sh
A shell parser, formatter, and interpreter with bash support; includes shfmt -
go-humanize
Go Humans! (formatters for units to human friendly sizes) -
bluemonday
bluemonday: a fast golang HTML sanitizer (inspired by the OWASP Java HTML Sanitizer) to scrub user generated content of XSS -
commonregex
🍫 A collection of common regular expressions for Go -
html-to-markdown
⚙️ Convert HTML to Markdown. Even works with entire websites and can be extended through rules. -
mxj
Decode / encode XML to/from map[string]interface{} (or JSON); extract values with dot-notation paths and wildcards. Replaces x2j and j2x packages. -
xpath
XPath package for Golang, supports HTML, XML, JSON document query. -
omniparser
omniparser: a native Golang ETL streaming parser and transform library for CSV, JSON, XML, EDI, text, etc. -
go-pkg-rss
This package reads RSS and Atom feeds and provides a caching mechanism that adheres to the feed specs. -
goribot
A simple golang spider/scraping framework,build a spider in 3 lines. -
goq
A declarative struct-tag-based HTML unmarshaling or scraping package for Go built on top of the goquery library -
xquery
XQuery lets you extract data from HTML/XML documents using XPath expression. -
github_flavored_markdown
GitHub Flavored Markdown renderer with fenced code block highlighting, clickable header anchor links. -
go-pkg-xmlx
Extension to the standard Go XML package. Maintains a node tree that allows forward/backwards browsing and exposes some simple single/multi-node search functions. -
go-zero-width
Zero-width character detection and removal for Go -
pagser
Pagser is a simple, extensible, configurable parse and deserialize html page to struct based on goquery and struct tags for golang crawler -
bafi
Universal JSON, BSON, YAML, CSV, XML, mt940 converter with templates -
csvplus
csvplus extends the standard Go encoding/csv package with fluent interface, lazy stream operations, indices and joins. -
gonameparts
Takes a full name and splits it into individual name parts -
codetree
:evergreen_tree: Parses indented code and returns a tree structure. -
jsoncolor
Colorized JSON output for Go https://godoc.org/github.com/nwidger/jsoncolor
InfluxDB - Power Real-Time Data Analytics at Scale
Do you think we are missing an alternative of go-fixedwidth or a related project?
README
fixedwidth
Package fixedwidth provides encoding and decoding for fixed-width formatted Data.
go get github.com/ianlopshire/go-fixedwidth
Usage
Struct Tags
The struct tag schema schema used by fixedwidth is: fixed:"{startPos},{endPos},[{alignment},[{padChar}]]"
1.
The startPos
and endPos
arguments control the position within a line. startPos
and endPos
must both be positive integers greater than 0. Positions start at 1. The interval is inclusive.
The alignment
argument controls the alignment of the value within it's interval. The valid options are default
2, right
, and left
. The alignment
is optional and can be omitted.
The padChar
argument controls the character that will be used to pad any empty characters in the interval after writing the value. The default padding character is a space. The padChar
is optional and can be omitted.
Fields without tags are ignored.
Encode
// 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"`
Age uint `fixed:"31,33"`
}{
{1, "Ian", "Lopshire", 99.5, 20},
}
data, err := fixedwidth.Marshal(people)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s", data)
// Output:
// 1 Ian Lopshire 99.5020
Decode
// 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"`
Age uint `fixed:"31,33"`
}
// define some fixed-with data to parse
data := []byte("" +
"1 Ian Lopshire 99.50 20" + "\n" +
"2 John Doe 89.50 21" + "\n" +
"3 Jane Doe 79.50 22" + "\n")
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 Grade:99.5 Age:20}
//{ID:2 FirstName:John LastName:Doe Grade:89.5 Age:21}
//{ID:3 FirstName:Jane LastName:Doe Grade:79.5 Age:22}
It is also possible to read data incrementally
decoder := fixedwidth.NewDecoder(bytes.NewReader(data))
for {
var element myStruct
err := decoder.Decode(&element)
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
handle(element)
}
If you have an input where the indices are expressed in unicode codepoints, and not raw bytes fixedwidth supports this. Your data must be UTF-8 encoded:
decoder := fixedwidth.NewDecoder(strings.NewReader(data))
decoder.SetUseCodepointIndices(true)
// Decode as usual now
Alignment Behavior
Alignment | Encoding | Decoding |
---|---|---|
default |
Field is left aligned | The padding character is trimmed from both right and left of value |
left |
Field is left aligned | The padding character is trimmed from right of value |
right |
Field is right aligned | The padding character is trimmed from left of value |
Notes
{}
indicates an argument.[]
indicates and optional segment [](#a1)- The
default
alignment is similar toleft
but has slightly different behavior required to maintain backwards compatibility [](#a2)
Licence
MIT