atomicwrite

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2022 License: BSD-3-Clause Imports: 11 Imported by: 0

README

go-atomicwrite

Go package to implement an atomic io.WriteCloser instance backed by the gocloud.dev/blob package.

Documentation

Go Reference

Example

import (
	"context"
	"fmt"
	"github.com/sfomuseum/go-atomicwrite"
)

func main(){

	ctx := context.Background()
	
	fname := "atomicwrite.txt"
	uri := fmt.Sprintf("mem://%s", fname)

	wr, _ := atomicwrite.New(ctx, uri)
	wr.Write([]byte("Hello world"))
	wr.Close()
}

Error handling omitted for the sake of brevity.

See also

Documentation

Overview

package atomicwrite implements an atomic io.WriteCloser instance backed by the gocloud.dev/blob package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(ctx context.Context, uri string) (io.WriteCloser, error)

New returns a new AtomicWriter instance. 'uri' is expected to a valid gocloud.dev/blob URI however if 'uri' is passed in as a schema-less Unix-style path it will be converted to a gocloud.dev/blob `file://` URI. Under the hood this method will attempt to create a new temporary file for the "path" element of URI whose filename will be appended with a random string. This temporary file is where data will be written to until the `Close` method is invoked at which point the data in the temporary file will be copied to the final path (defined by 'uri') and the temporary file will be removed. This method will create a new `blob.Writer` instance (which implements `io.WriteCloser`) with the default nil `blob.WriterOptions`. If you need to specify custom writer options you should use the `NewWithOptions` method.

func NewWithOptions

func NewWithOptions(ctx context.Context, uri string, writer_opts *blob.WriterOptions) (io.WriteCloser, error)

NewWithOptions returns a new AtomicWriter instance, specifying 'writer_opts' as the custom options used to create the underlying `blob.Writer` instance.

Types

type AtomicWriter

type AtomicWriter struct {
	io.WriteCloser
	// contains filtered or unexported fields
}

type AtomicWriter implements an atomic io.WriteCloser instance backed by the gocloud.dev/blob package.

func (*AtomicWriter) Close

func (aw *AtomicWriter) Close() error

Close will copy data written to the intermediate temporary file to the final path defined in the `New` constructor. Upon successfully completing this operation the temporary file will be removed.

func (*AtomicWriter) Write

func (aw *AtomicWriter) Write(b []byte) (int, error)

Write writes 'b' to the underlying writer instance.

Jump to

Keyboard shortcuts

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