GithubHelp home page GithubHelp logo

stob's Introduction

stob - convert struct to bytes and back

stob is package for convert structs to bytes or fill structs from bytes, as it does on C, i.e convert raw bytes to struct, and restore struct from raw bytes.

Unfortunately go does not allow do it also as simple as it can be done on C. There are 3 solutions:

  1. use unsafe package - this is very fast, but not flexibly.

  2. generatable code - it`s also fast, but it is not convenient.

  3. reflection - it`s relatively slow...

stob use reflection for read and write struct to bytes.

Install

go get github.com/sg3des/stob

Example

type YourStruct struct {
	Str      string 
	Int      int `bo:"le"`
	Byte     byte
	Bytes    []byte `num:"8"`
	Bytes4   [4]byte
	Bool     bool
	Float    float32 `bo:"be"`
}

a := YourStruct{
	Str:      "string",
	Int:      999,
	Byte:     255,
	Bytes:    []byte{1, 2, 3, 4, 5, 6, 7, 8},
	Bytes4:   [4]byte{10, 11, 12, 13},
	Bool:     rand.Intn(2) == 1,
	Float:    rand.Float32(),
}

stob.Write(w, a)

it will be write how:

73 74 72 69 6e 67 00 e7  03 00 00 00 00 00 00 ff  |string..........|
01 02 03 04 05 06 07 08  0a 0b 0c 0d 01 3f 70 c5  |.............?p.|
34                                                |4|

to restore struct from bytes:

stob.Read(r, &a)

Tags

stob knows 3 tags:

  • bo:"le" or bo:"be" - it`s byte order little or big endian
  • num:"8" - count of elements in slice
  • size:"4" - size of element, example size of string, but it also allows read\write big integers to small number of bytes.

WARNING: if []byte slice does not have num tag, then all next bytes will be writed to this field!

Benchmark

BenchmarkRead-8    2000000    762 ns/op    10 B/op    10 allocs/op
BenchmarkWrite-8   2000000    750 ns/op    24 B/op    6 allocs/op

unsafe:
BenchmarkRead-8    10000000   205 ns/op    0 B/op     0 allocs/op
BenchmarkWrite-8   5000000    257 ns/op    16 B/op    1 allocs/op

TODO

  • types
  • tests
  • reader and writer for custom types

stob's People

Contributors

sg3des avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.