fixedwidth

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2019 License: MIT Imports: 7 Imported by: 1

README

Fixedwidth

Build Status Report Code coverage

Fixedwidth is a Go package that provides a simple way to define fixed-width data, fast encoding and decoding also is the project's target.

Character encoding supported

UTF-8

Getting Started

Installation

To start using Fixedwidth, run go get:

$ go get github.com/huydang284/fixedwidth
How we limit a struct field

To limit a struct field, we use fixed tag.

Example:

type people struct {
    Name string `fixed:"10"`
    Age  int    `fixed:"3"`
}

If the value of struct field is longer than the limit that we defined, redundant characters will be truncated.

Otherwise, if the value of struct field is less than the limit, additional spaces will be appended.

Encoding

We can use Marshal function directly to encode fixed-width data.

package main

import (
    "fmt"
    "github.com/huydang284/fixedwidth"
)

type people struct {
    Name string `fixed:"10"`
    Age  int    `fixed:"3"`
}

func main() {
    me := people {
        Name: "Huy",
        Age: 25,
    }
    data, _ := fixedwidth.Marshal(me)
    fmt.Println(string(data))
}

The result will be:

Huy       25 
Decoding

For decoding, we use Unmarshal.

package main

import (
    "fmt"
    "github.com/huydang284/fixedwidth"
)

type people struct {
    Name string `fixed:"10"`
    Age  int    `fixed:"3"`
}

func main() {
    var me people
    data := []byte("Huy       25 ")
    fixedwidth.Unmarshal(data, &me)
    fmt.Printf("%+v", me)
}

The result will be:

{Name:Huy Age:25}

Author

Huy Dang (huydangg28@gmail.com)

License

Fixedwidth source code is available under the MIT License.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Marshal

func Marshal(v interface{}) ([]byte, error)

Marshal see Marshal method of Marshaler

func Unmarshal

func Unmarshal(data []byte, v interface{}) error

Unmarshal see Unmarshal method of Unmarshaler

Types

type Marshaler

type Marshaler struct {
	// contains filtered or unexported fields
}

Marshaler is the place fixed-width encoding happen

func NewMarshaler

func NewMarshaler() *Marshaler

NewMarshaler create new Marshaler When creating new Marshaler, you should consider b field

func (*Marshaler) Marshal

func (m *Marshaler) Marshal(v interface{}) ([]byte, error)

Marshal returns the fixed-width encoding of v.

v should be struct or a slice of struct.

Each field in a struct need to be defined a `fixed` tag. The `fixed` tag indicates the maximum width of current field.

If v is slice of struct, Marshal will return multi lines separated by new line character (\n).

Example
p := person{
	FirstName: "Alexander",
	LastName:  "Goodword",
	Age:       40,
	Job:       "Software Engineer",
}
m := NewMarshaler()
b, err := m.Marshal(p)
if err != nil {
	log.Fatal(err)
}
fmt.Print(string(b))
Output:

Alexander Goodword  40  Software

type Unmarshaler

type Unmarshaler struct {
	// contains filtered or unexported fields
}

Unmarshaler is the place fixed-width decoding happen

func NewUnmarshaler

func NewUnmarshaler() Unmarshaler

NewUnmarshaler create new Unmarshaler

func (Unmarshaler) Unmarshal

func (m Unmarshaler) Unmarshal(data []byte, model interface{}) error

Unmarshal decodes fixed-width encoding data to model, model is required to be a pointer.

Example
var p person
m := NewUnmarshaler()
err := m.Unmarshal([]byte("Alexander Goodword  40  Software"), &p)
if err != nil {
	log.Fatal(err)
}
fmt.Printf("%+v", p)
Output:

{FirstName:Alexander LastName:Goodword Age:40 Job:Software}

Jump to

Keyboard shortcuts

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