Skip to content

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

License

Notifications You must be signed in to change notification settings

aler9/writerseeker

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 100.0%