Skip to content

henglory/go-fixedwidth

 
 

Repository files navigation

fixedwidth GoDoc Report card Go Cover

Package fixedwidth provides encoding and decoding for fixed-width formatted Data.

go get github.com/ianlopshire/go-fixedwidth

Usage

Struct Tags

Position within a line is controlled via struct tags. The tags should be formatted as fixed:"{startPos},{endPos}" where startPos and endPos are both positive integers greater than 0. Positions start at 1. The interval is inclusive. 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"`
		Remark    string  `fixed:"26,32" cp:"Windows-874"`
		Grade     float64 `fixed:"33,38"`
	}{
		{1, "Ian", "Lopshire", "ทดสอบ", 99.5},
	}

	data, err := fixedwidth.Marshal(people)
	if err != nil {
		log.Fatal(err)
	}

	ms874Reader := bytes.NewBuffer(data)
	reader, err := charset.NewReaderLabel("Windows-874", ms874Reader)
	if err != nil {
		log.Fatal(err)
	}
	nb, err := ioutil.ReadAll(reader)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("%s", nb)
// Output:
// 1    Ian       Lopshire  ทดสอบ  99.50

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"`
}

// 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 := 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}
//{ID:2 FirstName:John LastName:Doe Grade:89.5}
//{ID:3 FirstName:Jane LastName:Doe Grade:79.5}

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)
}

Licence

MIT