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 year 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 (about 5 years ago)
- Default Branch: main
- Last Pushed: 2025-05-05T08:08:45.000Z (about 1 year ago)
- Last Synced: 2025-05-06T12:59:54.528Z (about 1 year ago)
- Topics: buffer-management
- Language: Go
- Homepage:
- Size: 92.8 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# buffers-pool
[](https://github.com/sv-tools/buffers-pool/actions/workflows/checks.yaml)
[](https://pkg.go.dev/github.com/sv-tools/buffers-pool)
[](https://codecov.io/gh/sv-tools/buffers-pool)
[](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.