writerseeker

package module
v0.0.0-...-1d3f536 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2020 License: MIT Imports: 3 Imported by: 68

README

WriterSeeker CircleCI GoDoc

WriterSeeker is the in-memory io.WriteSeeker implementation missing in the standard lib :-)

Use-case

In serverless / PaaS environments there is usually no file system access - you cannot read or write files to the container you are running in. This means that if you are using a function or library in Go that expects a File type (which implements the io.WriteSeeker interface), you are pretty much screwed. WriterSeeker solves this by letting you write and seek inside an in-memory buffer.

Usage Example

Let's say that you are using a library to generate PDF files. The library usually expects a File type to perform the writing to. You would create a File by using os.Open and then feed this to the Write function like so:

fWrite, err := os.Create(outputPath)
if err != nil {
    return err
}

defer fWrite.Close()

err = pdfWriter.Write(fWrite)

With WriterSeeker, you do not need the file, just work in-memory:

writerSeeker := &writerseeker.WriterSeeker{}
err = pdfWriter.Write(writerSeeker)

Now you can get a an io.Reader from the writerSeeker instance and boogie! for example, copy it's buffer to an io.Writer.

r := writerSeeker.Reader()
w := getWriter()
if _, err := io.Copy(w, r); err != nil {
 ...
 ...
 ...
}

License

The code is MIT licensed. It uses code from this post on StackOverflow, and according to the official docs this code can be safely used with MIT license.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type WriterSeeker

type WriterSeeker struct {
	// contains filtered or unexported fields
}

WriterSeeker is an in-memory io.WriteSeeker implementation

func (*WriterSeeker) BytesReader

func (ws *WriterSeeker) BytesReader() *bytes.Reader

BytesReader returns a *bytes.Reader. Use it when you need a reader that implements the io.ReadSeeker interface

func (*WriterSeeker) Close

func (ws *WriterSeeker) Close() error

Close :

func (*WriterSeeker) Reader

func (ws *WriterSeeker) Reader() io.Reader

Reader returns an io.Reader. Use it, for example, with io.Copy, to copy the content of the WriterSeeker buffer to an io.Writer

func (*WriterSeeker) Seek

func (ws *WriterSeeker) Seek(offset int64, whence int) (int64, error)

Seek seeks in the buffer of this WriterSeeker instance

func (*WriterSeeker) Write

func (ws *WriterSeeker) Write(p []byte) (n int, err error)

Write writes to the buffer of this WriterSeeker instance

Jump to

Keyboard shortcuts

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