Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/sv-tools/buffers-pool
- Owner: sv-tools
- License: mit
- Created: 2021-04-07T17:16:13.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-08-22T07:41:08.000Z (4 months ago)
- Last Synced: 2024-08-22T08:52:43.979Z (4 months ago)
- Topics: buffer-management
- Language: Go
- Homepage:
- Size: 66.4 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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 mainimport (
"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.