Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/sv-tools/buffers-pool

Go Pool of Byte Buffers
https://github.com/sv-tools/buffers-pool

buffer-management

Last synced: about 1 month ago
JSON representation

Go Pool of Byte Buffers

Awesome Lists containing this project

README

        

# buffers-pool
[![Code Analysis](https://github.com/sv-tools/buffers-pool/actions/workflows/checks.yaml/badge.svg)](https://github.com/sv-tools/buffers-pool/actions/workflows/checks.yaml)
[![Go Reference](https://pkg.go.dev/badge/github.com/sv-tools/buffers-pool.svg)](https://pkg.go.dev/github.com/sv-tools/buffers-pool)
[![codecov](https://codecov.io/gh/sv-tools/buffers-pool/branch/main/graph/badge.svg?token=0XVOTDR1CW)](https://codecov.io/gh/sv-tools/buffers-pool)
[![GitHub tag (latest SemVer)](https://img.shields.io/github/v/tag/sv-tools/buffers-pool?style=flat)](https://github.com/sv-tools/buffers-pool/releases)

The small library with an implementation of Buffer Pool.
The library was created to avoid repeating this code.

Here is a good article how to implement and properly use the Buffer Pools: https://www.captaincodeman.com/2017/06/02/golang-buffer-pool-gotcha

Check the [tests](pool_test.go) file for some examples.

PS: The package is stable and the new releases are not expected.

## Usage

```go
package main

import (
"fmt"
"sync"
"text/template"

buffferspool "github.com/sv-tools/buffers-pool"
)

func render(tmpl string, data interface{}) (string, error) {
tp, err := template.New("test").Parse(tmpl)
if err != nil {
return "", err
}

buf := buffferspool.Get()
defer buffferspool.Put(buf)

if err := tp.Execute(buf, data); err != nil {
return "", err
}

// the usage of buf.String is safe
return buf.String(), nil
}

func main() {
var tmpl string
var data []interface{}
// ... load template and data to variables ...

var wg sync.WaitGroup
res := make(chan string, len(data))
for _, d := range data {
wg.Add(1)
go func(data interface{}) {
defer wg.Done()
val, err := render(tmpl, data)
if err != nil {
res <- err.Error()
return
}

res <- val
}(d)
}

wg.Wait()
close(res)

for val := range res {
fmt.Println(val)

}
}
```

## License

MIT licensed. See the bundled [LICENSE](LICENSE) file for more details.