Documentation
¶
Overview ¶
Package pool provides sync.Pool utilities for pooled hash functions and bytes.Buffers.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BuffCloser ¶
BuffCloser represents a pooled bytes.Buffer.
func GetBuffer ¶
func GetBuffer() *BuffCloser
GetBuffer returns a pooled bytes.Buffer and a reset function.
Example ¶
package main
import (
"fmt"
"io"
"strings"
"github.com/mwalto7/pool"
)
func main() {
// Get a bytes.Buffer from the pool.
buf := pool.GetBuffer()
defer buf.Close()
// Do something with the buffer.
io.Copy(buf, strings.NewReader("hello, world"))
fmt.Println(buf.String())
}
Output: hello, world
func (*BuffCloser) Close ¶
func (b *BuffCloser) Close() error
Close resets the buffer and puts the buffer back into the pool.
type HashCloser ¶
type HashCloser interface {
hash.Hash
// Close resets the hash to its initial state and puts it back into the pool.
Close() error
}
HashCloser represents a pooled hash.Hash.
func GetHash ¶
func GetHash(h crypto.Hash) HashCloser
GetHash returns a pooled hash function.
Example (Multiple) ¶
package main
import (
"crypto"
"encoding/hex"
"fmt"
"io"
"strings"
"github.com/mwalto7/pool"
)
func main() {
// Get a SHA256 hash function from the pool.
sha256 := pool.GetHash(crypto.SHA256)
defer sha256.Close()
// Get an MD5 hash function from the pool.
md5 := pool.GetHash(crypto.MD5)
defer md5.Close()
// Hash something with the hash functions.
io.Copy(io.MultiWriter(sha256, md5), strings.NewReader("hello, world"))
// Get the hash sums.
fmt.Println("SHA256:", hex.EncodeToString(sha256.Sum(nil)))
fmt.Println("MD5:", hex.EncodeToString(md5.Sum(nil)))
}
Output: SHA256: 09ca7e4eaa6e8ae9c7d261167129184883644d07dfba7cbfbc4c8a2e08360d5b MD5: e4d7f1b4ed2e42d15898f4b27b019da4
Example (Sha256) ¶
package main
import (
"crypto"
"encoding/hex"
"fmt"
"io"
"strings"
"github.com/mwalto7/pool"
)
func main() {
// Get a SHA256 hash function from the pool.
h := pool.GetHash(crypto.SHA256)
defer h.Close() // Dont forget to call Close! This puts the hash function back into the pool.
// Hash something.
io.Copy(h, strings.NewReader("hello, world"))
// Get the hash sum.
sum := h.Sum(nil)
fmt.Println("SHA256:", hex.EncodeToString(sum))
}
Output: SHA256: 09ca7e4eaa6e8ae9c7d261167129184883644d07dfba7cbfbc4c8a2e08360d5b
Click to show internal directories.
Click to hide internal directories.