Skip to content

Latest commit

 

History

History

Folders and files

README.md

encode

Go Reference

Package encode serializes and deserializes values crossing system boundaries such as databases, queues, caches, and RPC payloads.

The package supports two main modes:

  • Gob + Base64 encoding for arbitrary Go values
  • JSON + Base64 encoding for interoperable text-based payloads

Caveats:

  • Decoding reads a single encoded value; any trailing bytes are ignored.
  • The reader- and buffer-based decoders do not bound their input. When decoding from an untrusted source, wrap the reader with io.LimitReader.
  • Gob is not designed to decode adversarial input; prefer the JSON family (Serialize/Deserialize) for untrusted, interoperable payloads.
  • The JSON encoding appends a trailing newline, which is included in the encoded output.

When To Use

  • The same payload shape moves between a database, a queue, and a cache.
  • You want one encoding decision rather than one per call site.

Example

type TestData struct {
	Alpha string
	Beta  int
}

data := &TestData{Alpha: "test_string", Beta: -9876}

v, err := encode.Encode(data)
if err != nil {
	log.Fatal(err)
}

fmt.Println(v)

Full source is in example_encode_test.go. More runnable examples are on pkg.go.dev.

Dependencies

This package reaches no external module: it uses only the Go standard library.

Links