iobp

package
v0.9.17 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2024 License: BSD-3-Clause Imports: 1 Imported by: 0

Documentation

Overview

Package iobp provides useful implementations of interfaces defined in stdlib io package.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CountingSink

type CountingSink int64

CountingSink is an io.Writer implementation that discards all incoming data but tracks how many bytes were "written".

This can be used as a sink for encoders or as an additional output via a MultiWriter to track data sizes without buffering all of the data in memory.

A Write to a CountingSink cannot fail. A CountingSink is not safe for concurrent use.

Example

This example demonstrates how to use CountingSink to count the size of a json-serialized object.

package main

import (
	"encoding/json"
	"fmt"

	"github.com/reddit/baseplate.go/iobp"
)

func main() {
	// The json string would be "[0,1,2,3,4]\n", so the size should be 12.
	object := []int{0, 1, 2, 3, 4}

	var sink iobp.CountingSink
	if err := json.NewEncoder(&sink).Encode(object); err != nil {
		panic(err)
	}
	fmt.Printf("JSON size for %#v: %d\n", object, sink.Size())

}
Output:

JSON size for []int{0, 1, 2, 3, 4}: 12

func (CountingSink) Size

func (cs CountingSink) Size() int64

Size returns the current counted size.

func (*CountingSink) Write

func (cs *CountingSink) Write(buf []byte) (int, error)

Jump to

Keyboard shortcuts

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