msgpack

package module
v0.0.0-...-f7050a3 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decoder

func Decoder[T any](b []byte, v *T) error

Decoder decodes msgpack bytes into T (tinylib). T must have msgp code generation (go:generate msgp) so that *T implements msgp.Unmarshaler.

func Encoder

func Encoder[T any](v T) ([]byte, error)

Encoder encodes T to msgpack bytes (tinylib). T must have msgp code generation (go:generate msgp) so that *T implements msgp.Marshaler.

func NewCodec

func NewCodec[T any]() encoding.Codec[T, []byte]

NewCodec returns a msgpack codec for T backed by tinylib/msgp. T must have msgp code generation (go:generate msgp) so that *T implements msgp.Marshaler and msgp.Unmarshaler. It is safe for concurrent use.

Example
package main

import (
	"fmt"

	msgpack "github.com/foomo/goencode/msgpack/tinylib"
	"github.com/foomo/goencode/msgpack/tinylib/testdata"
)

func main() {
	c := msgpack.NewCodec[testdata.User]()

	encoded, err := c.Encode(*testdata.NewUserTinyLib())
	if err != nil {
		fmt.Printf("Encode failed: %v\n", err)
		return
	}

	var decoded testdata.User
	if err := c.Decode(encoded, &decoded); err != nil {
		fmt.Printf("Decode failed: %v\n", err)
		return
	}

	fmt.Printf("Decoded Handle: %s\n", decoded.Handle)
	fmt.Printf("Decoded Country: %s\n", decoded.Country)
}
Output:
Decoded Handle: @bench
Decoded Country: US

func NewStreamCodec

func NewStreamCodec[T any]() encoding.StreamCodec[T]

NewStreamCodec returns a msgpack stream codec for T backed by tinylib/msgp. T must have msgp code generation (go:generate msgp) so that *T implements msgp.Encodable and msgp.Decodable. It is safe for concurrent use.

Example
package main

import (
	"bytes"
	"fmt"

	msgpack "github.com/foomo/goencode/msgpack/tinylib"
	"github.com/foomo/goencode/msgpack/tinylib/testdata"
)

func main() {
	c := msgpack.NewStreamCodec[testdata.User]()

	var buf bytes.Buffer
	if err := c.Encode(&buf, *testdata.NewUserTinyLib()); err != nil {
		fmt.Printf("Encode failed: %v\n", err)
		return
	}

	var decoded testdata.User
	if err := c.Decode(&buf, &decoded); err != nil {
		fmt.Printf("Decode failed: %v\n", err)
		return
	}

	fmt.Printf("Decoded Handle: %s\n", decoded.Handle)
	fmt.Printf("Decoded Country: %s\n", decoded.Country)
}
Output:
Decoded Handle: @bench
Decoded Country: US

func StreamDecoder

func StreamDecoder[T any](r io.Reader, v *T) error

StreamDecoder decodes T from a msgpack stream (tinylib). T must have msgp code generation (go:generate msgp) so that *T implements msgp.Decodable.

func StreamEncoder

func StreamEncoder[T any](w io.Writer, v T) error

StreamEncoder encodes T to a msgpack stream (tinylib). T must have msgp code generation (go:generate msgp) so that *T implements msgp.Encodable.

Types

This section is empty.

Jump to

Keyboard shortcuts

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