Golang

Using Polyglot in Golang

Installation

go get github.com/loopholelabs/polyglot/v2

Basic Usage

Encoding Data

// Create a new buffer
p := NewBuffer()
 
// Create an encoder
e := Encoder(p)
 
// Encode values
e.String("Hello World")
e.Uint32(42)
e.Bool(true)
 
// Get the encoded bytes
bytes := p.Bytes()

Decoding Data

// Create a decoder from bytes
d := Decoder(bytes)
 
// Decode values
str, err := d.String()
if err != nil {
    // Handle error
}
 
num, err := d.Uint32()
if err != nil {
    // Handle error
}
 
b, err := d.Bool()
if err != nil {
    // Handle error
}

Memory Management

Polyglot's Go implementation includes special features for memory efficiency:

Buffer Pooling

The Buffer type supports reuse to minimize allocations:

p := NewBuffer()
defer p.Reset() // Return buffer to pool
 
// Use buffer...
e := Encoder(p)
e.String("Hello")
 
// Reset clears the buffer for reuse
p.Reset()
 
// Reuse the same buffer
e.String("World")

Allocation Optimizations

Monitor allocations in performance-critical code:

test.go
p := NewBuffer()
n := testing.AllocsPerRun(100, func() {
    Encoder(p).String("test")
    d := Decoder(p.Bytes())
    val, _ := d.String()
    p.Reset()
})
// Expect minimal allocations

Working with Collections

Arrays

// Encoding arrays
e := Encoder(p)
data := []string{"1", "2", "3"}
 
e.Slice(uint32(len(data)), StringKind)
for _, v := range data {
    e.String(v)
}
 
// Decoding arrays
d := Decoder(bytes)
size, err := d.Slice(StringKind)
if err != nil {
    // Handle error
}
 
result := make([]string, size)
for i := range result {
    result[i], err = d.String()
    if err != nil {
        // Handle error
    }
}
Edit on GitHub

Last updated on

On this page