Simple package to marshall and unmarshall fixed width text to struct
How to use:
package main
import (
"encoding/json""fmt""github.com/iarief/tts"
)
typeUserstruct {
FirstNamestring`txt_width:"10" pad_dir:"right" pad_str:" "`LastNamestring`txt_width:"10" pad_dir:"right" pad_str:" "`IgnoredValuestring`txt_width:"-" json:"-"`Ageint`txt_width:"3" pad_dir:"left" pad_str:"0"`Heightfloat64`txt_width:"8" pad_dir:"left" pad_str:" "`
}
funcmain() {
user:=User{"john", "doe", "ignore this", 20, 182.88}
str, _:=tts.Marshal(&user)
fmt.Println(str) // "john doe 020 182.88"user2:=User{}
_=tts.Unmarshal(str, &user2)
byteJSON, _:=json.Marshal(user2)
fmt.Println(string(byteJSON)) // "{\"FirstName\":\"john\",\:LastName\":\"doe\",\"Age\":20,\"Height\":182.88}"
}Please note while marshaling the string is forced to be the length that is defined in txt_width tag, either by substring or padding (which could be configured in pad tag)