encode

package
v1.122.2 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package encode provides a collection of functions for safe serialization and deserialization of data between different systems, such as databases, queues, and caches.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Base64EncodeString added in v1.121.0

func Base64EncodeString(s string) string

Base64EncodeString encodes a string in Base64.

func Base64Encoder added in v1.121.0

func Base64Encoder(w io.Writer) io.WriteCloser

Base64Encoder wraps an io.Writer with a base64 encoder.

func BufferDecode added in v1.121.0

func BufferDecode(reader io.Reader, data any) error

BufferDecode decodes gob+base64 data from the provided io.Reader into the provided data object.

func BufferDeserialize added in v1.121.0

func BufferDeserialize(reader io.Reader, data any) error

BufferDeserialize decodes JSON+base64 data from the provided io.Reader into the provided data object.

func BufferEncode added in v1.121.0

func BufferEncode(data any) (*bytes.Buffer, error)

BufferEncode encodes the input data to gob+base64 and returns it as a bytes.Buffer.

func BufferSerialize added in v1.121.0

func BufferSerialize(data any) (*bytes.Buffer, error)

BufferSerialize encodes the input data to JSON+base64 and returns it as a bytes.Buffer.

func ByteDecode

func ByteDecode(msg []byte, data any) error

ByteDecode decodes a byte slice message encoded with the ByteEncode function to the provided data object. The value underlying data must be a pointer to the correct type for the next data item received.

func ByteDeserialize

func ByteDeserialize(msg []byte, data any) error

ByteDeserialize decodes a string message encoded with the Serialize function to the provided data object. The value underlying data must be a pointer to the correct type for the next data item received.

func ByteEncode

func ByteEncode(data any) ([]byte, error)

ByteEncode encodes the input data to gob+base64 byte slice.

func ByteSerialize

func ByteSerialize(data any) ([]byte, error)

ByteSerialize encodes the input data to JSON+base64 byte slice.

func Decode

func Decode(msg string, data any) error

Decode decodes a string message encoded with the Encode function to the provided data object. The value underlying data must be a pointer to the correct type for the next data item received.

Example
package main

import (
	"fmt"
	"log"

	"github.com/tecnickcom/gogen/pkg/encode"
)

func main() {
	type TestData struct {
		Alpha string
		Beta  int
	}

	var data TestData

	msg := "Kf+BAwEBCFRlc3REYXRhAf+CAAECAQVBbHBoYQEMAAEEQmV0YQEEAAAAD/+CAQZhYmMxMjMB/gLtAA=="

	err := encode.Decode(msg, &data)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(data)

}
Output:

{abc123 -375}

func Deserialize

func Deserialize(msg string, data any) error

Deserialize decodes a byte slice message encoded with the Serialize function to the provided data object. The value underlying data must be a pointer to the correct type for the next data item received.

Example
package main

import (
	"fmt"
	"log"

	"github.com/tecnickcom/gogen/pkg/encode"
)

func main() {
	type TestData struct {
		Alpha string
		Beta  int
	}

	var data TestData

	msg := "eyJBbHBoYSI6ImFiYzEyMyIsIkJldGEiOi0zNzV9Cg=="

	err := encode.Deserialize(msg, &data)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(data)

}
Output:

{abc123 -375}

func Encode

func Encode(data any) (string, error)

Encode encodes the input data to gob+base64 string.

Example
package main

import (
	"fmt"
	"log"

	"github.com/tecnickcom/gogen/pkg/encode"
)

func main() {
	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)
}

func GobEncoder added in v1.121.0

func GobEncoder(enc io.WriteCloser, data any) error

GobEncoder encodes data using gob encoding and writes it to the provided io.WriteCloser.

func JsonEncoder added in v1.121.0

func JsonEncoder(enc io.WriteCloser, data any) error

JsonEncoder encodes data using JSON encoding and writes it to the provided io.WriteCloser.

func Serialize

func Serialize(data any) (string, error)

Serialize encodes the input data to JSON+base64 string.

Example
package main

import (
	"fmt"
	"log"

	"github.com/tecnickcom/gogen/pkg/encode"
)

func main() {
	type TestData struct {
		Alpha string
		Beta  int
	}

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

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

	fmt.Println(v)

}
Output:

eyJBbHBoYSI6InRlc3Rfc3RyaW5nIiwiQmV0YSI6LTk4NzZ9Cg==

Types

This section is empty.

Jump to

Keyboard shortcuts

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