Categorygithub.com/go-restruct/restruct
modulepackage
1.2.0-alpha
Repository: https://github.com/go-restruct/restruct.git
Documentation: pkg.go.dev

# README

restruct Build Status codecov.io godoc.org Go Report Card

restruct is a library for reading and writing binary data in Go. Similar to lunixbochs struc and encoding/binary, this library reads data based on the layout of structures and, like struc, based on what is contained in struct tags.

To install Restruct, use the following command:

go get github.com/go-restruct/restruct

restruct aims to provide a clean, flexible, robust implementation of struct packing. In the future, through fast-path optimizations and code generation, it also aims to be quick, but it is currently very slow.

restruct currently requires Go 1.7+.

Status

  • As of writing, coverage is hovering around 95%, but more thorough testing is always useful and desirable.
  • Unpacking and packing are fully functional.
  • More optimizations are probably possible.

Example

package main

import (
	"encoding/binary"
	"io/ioutil"
	"os"

	"github.com/go-restruct/restruct"
)

type Record struct {
	Message string `struct:"[128]byte"`
}

type Container struct {
	Version   int `struct:"int32"`
	NumRecord int `struct:"int32,sizeof=Records"`
	Records   []Record
}

func main() {
	var c Container

	file, _ := os.Open("records")
	defer file.Close()
	data, _ := ioutil.ReadAll(file)

	restruct.Unpack(data, binary.LittleEndian, &c)
}

# Packages

No description provided by the author
No description provided by the author

# Functions

BitSize returns the binary encoded size of the given value, in bits.
EnableExprBeta enables you to use restruct expr while it is still in beta.
Pack writes data from a datastructure into a byteslice.
RegisterArrayType is deprecated; it is now a noop.
SizeOf returns the binary encoded size of the given value, in bytes.
Unpack reads data from a byteslice into a value.

# Constants

DefaultFlag is set when the field is designated as a switch case default.
InvertedBoolFlag causes the true and false states of a boolean to be flipped in binary.
ParentFlag is set when the field points to the parent struct.
RootFlag is set when the field points to the root struct.
VariantBoolFlag causes the true value of a boolean to be ~0 instead of just 1 (all bits are set.) This emulates the behavior of VARIANT_BOOL.

# Variables

ErrInvalidBits is returned when bits is used on an invalid type.
ErrInvalidSize is returned when sizefrom is used on an invalid type.
ErrInvalidSizeFrom is returned when sizefrom is used on an invalid type.
ErrInvalidSizeOf is returned when sizefrom is used on an invalid type.

# Interfaces

BitSizer is an interface for types that need to specify their own size in bit-level granularity.
Packer is a type capable of packing a native value into a binary representation.
Sizer is a type which has a defined size in binary.
Unpacker is a type capable of unpacking a binary representation of itself into a native representation.

# Type aliases

FieldFlags is a type for flags that can be applied to fields individually.