Skip to content

o1egl/fwencoder

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Fixed width file parser (encoder/decoder) for GO (golang)

License GoDoc Build Status codecov Go Report Card

This library is using to parse fixed-width table data like:

Name            Address               Postcode Phone          Credit Limit Birthday
Evan Whitehouse V4560 Camel Back Road 3122     (918) 605-5383    1000000.5 19870101
Chuck Norris    P.O. Box 872          77868    (713) 868-6003     10909300 19651203

Install

To install the library use the following command:

$ go get -u github.com/o1egl/fwencoder

Decoding example

Parsing data from io.Reader:

type Person struct {
	Name        string
	Address     string
	Postcode    int
	Phone       string
	CreditLimit float64   `json:"Credit Limit"`
	Bday        time.Time `column:"Birthday" format:"20060102"`
}

f, _ := os.Open("/path/to/file")
defer f.Close

var people []Person
err := fwencoder.UnmarshalReader(f, &people)

You can also parse data from byte array:

b, _ := ioutil.ReadFile("/path/to/file")
var people []Person
err := fwencoder.Unmarshal(b, &people)

Encoding example

people := []Person{
	Name: "John",
	Address: "P.O. Box 872",
	Phone: "(713) 868-6003", 
	CreditLimit: 10909300,
	Bday: "19651203"
}

b, err := Marshal(&people)
fmt.Println(string(b))

or you can directly write to io.Writer

people := []Person{
	Name: "John",
	Address: "P.O. Box 872",
	Phone: "(713) 868-6003", 
	CreditLimit: 10909300,
	Bday: "19651203"
}

err := MarshalWriter(os.Stdout, &people)