Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/huydang284/fixedwidth
A Go package for encode/decode fixed-width data
https://github.com/huydang284/fixedwidth
decoding encoder-decoder encoding fixed-width fixedwidth go golang
Last synced: about 2 months ago
JSON representation
A Go package for encode/decode fixed-width data
- Host: GitHub
- URL: https://github.com/huydang284/fixedwidth
- Owner: huydang284
- License: mit
- Created: 2019-08-11T03:42:24.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2019-12-20T03:18:01.000Z (almost 5 years ago)
- Last Synced: 2024-07-31T20:52:53.836Z (4 months ago)
- Topics: decoding, encoder-decoder, encoding, fixed-width, fixedwidth, go, golang
- Language: Go
- Homepage:
- Size: 78.1 KB
- Stars: 9
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go - fixedwidth - Fixed-width text formatting (UTF-8 supported). (Serialization / HTTP Clients)
- zero-alloc-awesome-go - fixedwidth - Fixed-width text formatting (UTF-8 supported). (Serialization / HTTP Clients)
- awesome-go-extra - fixedwidth - width data|6|2|0|2019-08-11T03:42:24Z|2019-12-20T03:18:01Z| (Serialization / HTTP Clients)
README
# Fixedwidth
[![Build Status](https://travis-ci.org/huydang284/fixedwidth.svg?branch=master)](https://travis-ci.org/huydang284/fixedwidth)
[![Report](https://goreportcard.com/badge/github.com/huydang284/fixedwidth)](https://goreportcard.com/badge/github.com/huydang284/fixedwidth)
[![Code coverage](https://codecov.io/gh/huydang284/fixedwidth/branch/master/graph/badge.svg)](https://codecov.io/gh/huydang284/fixedwidth)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:
```go
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.```go
package mainimport (
"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`.```go
package mainimport (
"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 ([[email protected]](mailto:[email protected]))## License
Fixedwidth source code is available under the [MIT License](https://github.com/huydang284/fixedwidth/blob/master/LICENSE).