pool

package module
v0.0.0-...-1660b6a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 10, 2023 License: MIT Imports: 13 Imported by: 0

README

pool

GoDoc Go Report Card

Getting Started

go get -u github.com/mwalto7/pool

Example

package main

import (
	"crypto"
	"encoding/hex"
	"fmt"
	"io"
	"strings"

	"github.com/mwalto7/pool"
)

func main() {
    // Get a bytes.Buffer from the pool.
    buf := pool.GetBuffer()
    defer buf.Close() // Put the bytes.Buffer back into the pool!

    // Get a SHA256 hash function from the pool.
    h := pool.GetHash(crypto.SHA256)
    defer h.Close() // Put the hash function back into the pool!

    // Do something with the buffer and hash function.
    io.Copy(io.MultiWriter(buf, h), strings.NewReader("hello, world"))

    // Print the results.
    fmt.Printf("SHA256 %q: %s", buf.String(), hex.EncodeToString(h.Sum(nil)))
}
Output:
SHA256 "hello, world": 09ca7e4eaa6e8ae9c7d261167129184883644d07dfba7cbfbc4c8a2e08360d5b

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

type BuffCloser struct {
	*bytes.Buffer
}

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL